take value after execute query - sql

Im new to Codeigniter and its developing. i have tested a query succefully. But when i try to take voteid value im getting errors please help me.
$query=$this->db->query("SELECT voteid FROM Questions WHERE questionid = '$questionid'");
echo $query->voteid; //im getting errors here.
Var_dump value of query
object(CI_DB_mysql_result)#18 (8) {
["conn_id"]=>
resource(30) of type (mysql link persistent)
["result_id"]=>
resource(39) of type (mysql result)
["result_array"]=>
array(0) {
}
["result_object"]=>
array(0) {
}
["custom_result_object"]=>
array(0) {
}
["current_row"]=>
int(0)
["num_rows"]=>
int(1)
["row_data"]=>
NULL
Error
Severity: Notice
Message: Undefined property: CI_DB_mysql_result::$voteid
Filename: models/questions_model.php

$query=$this->db->query("SELECT * FROM Questions WHERE questionid = '$id'");
echo $query->row()->voteid;
try this it should work

Your query returns nothing because it's incorrect. Try: "SELECT voteid FROM Questions WHERE questionid = " . $questionid (I assume that $questionid is a variable of integer type)

Try this type of writing your queries:
$this->db->select( 'voteid' );
$this->db->from ( 'Questions' );
$this->db->where ( 'questionid', $questionid );
$result = $this->db->get( );
if ( $result->num_rows( ) > 0 )
{
$row = $result->row( );
$row->voteid;
}

Try this:
$query=$this->db->query( "SELECT voteid FROM Questions WHERE questionid = $questionid" );
$row = $query->row();
return $row->voteid;
Using '$questionid' means that your variable is a string, but in this case it's an integer.

Related

Can Laravel automatically switch between column = ? and column IS NULL depending on value?

When building a complex SQL query for Laravel, using ? as placeholders for parameters is great. However when the value is null, the SQL syntax needs to be changed from = ? to IS NULL. Plus, since the number of parameters is one less, I need to pass a different array.
To get it to work, I have written it like this, but there must be a better way:
if ($cohortId === null) {
// sql should be: column IS NULL
$sqlCohortString = "IS NULL";
$params = [
Carbon::today()->subDays(90),
// no cohort id here
];
} else {
// sql should be: column = ?
$sqlCohortString = "= ?";
$params = [
Carbon::today()->subDays(90),
$cohortId
];
}
$query = "SELECT items.`name`,
snapshots.`value`,
snapshots.`taken_at`,
FROM snapshots
INNER JOIN (
SELECT MAX(id) AS id, item_id
FROM snapshots
WHERE `taken_at` > ?
AND snapshots.`cohort_id` $sqlCohortString
GROUP BY item_id
) latest
ON latest.`id` = snapshots.`id`
INNER JOIN items
ON items.`id` = snapshots.`item_id`
ORDER by media_items.`slug` ASC
";
$chartData = DB::select($query, $params);
My question is: does Laravel have a way to detect null values and replace ? more intelligently?
PS: The SQL is for a chart, so I need the single highest snapshot value for each item.
You can use ->when to create a conditional where clause:
$data = DB::table('table')
->when($cohortId === null, function ($query) {
return $query->whereNull('cohort_id');
}, function ($query) use ($cohortId) {
// the "use" keyword provides access to "outer" variables
return $query->where('cohort_id', '=', $cohortId);
})
->where('taken_at', '>', $someDate)
->toSql();

Wordpress: Check data by using get_results

I have problem to check data if its found or not, Bellow i have table in Wordpress called (postmeta), and store data like :
meta_id : 27327
post_id : 4876
meta_key : rtb
meta_value : a:20:{s:5:"party";i:1;s:5:"email";s:18:"test#gmail.com";s:5:"phone";s:10:"05921655517";s:8:"catofres";s:6:"single";s:9:"tabletype";s:8:"openarea";s:14:"validationcode";i:8407704;s:11:"frindsname1";s:0:"";s:13:"frindsmobile1";s:0:"";s:11:"frindsname2";s:0:"";s:13:"frindsmobile2";s:0:"";s:11:"frindsname3";s:0:"";s:13:"frindsmobile3";s:0:"";s:11:"frindsname4";s:0:"";s:13:"frindsmobile4";s:0:"";s:11:"frindsname5";s:0:"";s:13:"frindsmobile5";s:0:"";s:11:"frindsname6";s:0:"";s:13:"frindsmobile6";s:0:"";s:15:"date_submission";i:1525176468;s:2:"ip";s:10:"37.8.85.11";}
Now the problem how can do SQL query that check if the data (test#gmail.com , 8407704) found it this table or not ,,,
I try this query :
$results = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}postmeta WHERE `meta_key` = 'rtb' ", OBJECT );
But I could not complete the check in the Query
global $wpdb;
$metas = $wpdb->get_results(
$wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta where meta_key = %s", 'rtb')
);
print_r($metas);
try also
if ( metadata_exists( 'post', $post_id, 'rtb' ) ) {
$meta_value = get_post_meta( $post_id, 'rtb', true );
}

Same column name used in where clause in codeigniter

i want to sql query like this
SELECT * FROM `tblName`
where `id`='007' and
`doj` != '2014-07-26' and
`doj` != '2014-08-04'
i want same column 'doj' used in where clause.
please help me?
Thanks in advance.
<?php
$dates = array(
'2014-07-26',
'2014-08-04'
);
$this->db->from('tblName');
$this->db->where('id','007');
// first variant
foreach($dates as $date){
$this->db->where('doj !=',$date);
}
// or second variant
$this->db->where_not_in('doj',$dates); // <- seems to be better
// finally get result
$result = $this->db->get();
if($result->num_rows()){
var_dump($result->result_array());
} else {
echo 'no rows found';
}
// check query
echo $this->db->last_query();
?>

sprintf and php

The following function gives all the related data except artist_id, when run I have checked all elements in the database and ok.
If I change the artist_id to an actual 'id' it shows in the result of the function as WHERE artist_id = 4 AND........
Confess I do not understand what is causing this.
Result of the function:
SELECT `image_album_id`, `member_id`, `artist_id`, `albumname`, `ext`, `timestamp`
FROM album_images WHERE artist_id = AND member_id = 1 AND
image_album_id = 160
<?php
function get_data_nxtprv($fields, $where) {
$return = FALSE;
// Template
$template = "SELECT %s "
. "FROM album_images "
. "WHERE artist_id = " . $artist_id. "
AND member_id = ".$_SESSION['member_id']." %s";
// Current record
$sql = sprintf($template, $fields, $where);
$query = mysql_query($sql);
$query_result = mysql_fetch_assoc($query);
//print_r($sql);
// If data has been found
if ($query_result)
{
$return = $query_result;
}
return $return;
?>
I am not entirely sure I understand your question. But I noticed that your function uses three input variables:
$artist_id, $fields, $where
But $artist_id is not getting passed as an argument.
You would need to modify the function call:
function get_data_nxtprv($artist_id, $fields, $where)
There is an error in your SQL
SELECT `image_album_id`, `member_id`, `artist_id`, `albumname`, `ext`, `timestamp`
FROM album_images WHERE artist_id = AND member_id = 1 AND
image_album_id = 160
should it not be
SELECT `image_album_id`, `member_id`, `artist_id`, `albumname`, `ext`, `timestamp`
FROM album_images WHERE member_id = 1 AND
image_album_id = 160
if artist_id is one of the fields you're looking for?

sql update codeigniter

I am using codeIgniter..
I want to update a table column is_close when id=$ticket_id of my table= tbl_tickets.
I am doing this :-
$data=array(
'is_close'=>1
);
$this->db->where('id',$title_id);
$this->db->update('tbl_tickets',$data);
and I have also done this :-
$sql = "UPDATE tbl_tickets SET is_close={1} WHERE id='$title_id'";
$this->db->query($sql);
both are not working,i.e., my table is not updating the value to 1 and also no error is being shown in the broswer. :(
Edited: Included my model part :
function setClosePost($title_id){
$sql = "UPDATE tbl_tickets SET is_close=0 WHERE id='$title_id'";
$this->db->query($sql);
// $data=array(
// 'is_close'=>1
// );
// $this->db->where('id',$title_id);
// $this->db->update('tbl_tickets',$data);
}
My controller :-
function closePost(){
$this->load->model('helpdesk_model');
$this->helpdesk_model->setClosePost($this->input->post('title_id'));
}
first of all use a get method to check if ticket_id is exist or not.
another thing is always use return in your functions in models so you can check them by if(function_name){...}else{...}
then if your get method returned data correctly try
Model Method
public function set_closed($ticket_id){
$this->db->set(array(
'is_close'=>1
)); // pass fields in array
$this->db->where('id',$ticket_id);
$this->db->update('tbl_tickets'); // table name
return true;
}
then check that in your controller
if($this->Ticket_model->set_closed($ticket_id) == true){
echo 'ticket set to closed correctly';
}else{
echo 'there is some error on updating database'.$this->db->error(); // to checkout db error .
}
First, check $title_id before passing:
var_dump($title_id);
Then, try do "select a row with this id" before updating and after.
$query = $this->db->get_where('tbl_tickets', array('id' => $id));
foreach ($query->result() as $row)
{
var_dump($row->is_close);
}
$data=array(
'is_close'=>1
);
$this->db->where('id',$title_id);
$this->db->update('tbl_tickets',$data);
$query = $this->db->get_where('tbl_tickets', array('id' => $id));
foreach ($query->result() as $row)
{
var_dump($row->is_close);
}
Then, give your table structure.
Just try like this
$sql = "UPDATE tbl_tickets SET is_close='1' WHERE id=".$title_id;
$this->db->query($sql);
just try like this
**function edit($close,$id) {
$sql = "UPDATE tbl_tickets SET is_close= ? WHERE id = ? ";
$this->db->query($sql, array($close,$id));
}**
To handle this type of errors, i mean if reflection is not happen in database, then use below steps to resolve this type of error.
1) use $this->db->last_query() function to print query, using this we can make sure our variable have correct value (should not null or undefined), using that we can make sure also SQL query is valid or not.
2) If SQL query is valid then open phpmyadmin & fire same query into phpmyadmin, it will return error if query columns or table names are invalid.
Use this way, its best way to cross check our SQL queries issues.
I hope it will work.
Thanks
You are trying to update integer(INT) type value, just cross check with your column datatype if that is varchar then you have to put value in a single or double quote.
Like this
$data=array('is_close'=> '1');