This question already has answers here:
getting the value of the single field output using the codeigniter active record
(5 answers)
Closed 2 years ago.
hi want to get only one column from one result from my database. So i do it in this way
$this->db->select('myclolumn')->where('id',$id);
$query = $this->db->get('mytable');
$temp=$query->row_array();
$finalresult=$temp['mycolumn'];
Maybe there is a easier or faster way to do the same?
so based on DanishAli's comment
$this->db->select('myclolumn')->where('id',$id);
$query = $this->db->get('mytable');
$finalresult=$query->row()->mycloumn;
You can do it like this:
$finalresult = $this->db->select('myclolumn')
->where('id',$id)
->get('mytable')->row()
->mycolumn;
All in only one command instead of a lot of variables and extra code.
In case you need to provide a default value if the row is null, break the command to:
$row = $this->db->select('myclolumn')
->where('id',$id)
->get('mytable')
->row();
$finalresult = ($row)?$row->mycolumn:'your default value';
Related
This question already has answers here:
The 3 different equals
(5 answers)
Closed 4 years ago.
I'm using a very simple code to extract data from a MySQL DB to a CSV file.
The code didn't provide the result I expected so I made the code including the query in the CSV file.
In a fist part of the code seemes that the variable containing the query result doesn't actually contain any value, but in another part of the code the variable contains the correct value.
In short, the same variable seems to contain two values in different part of the code.
$sql="SELECT destinazione AS dest,ndc AS n FROM npitz";
$query = mysql_query($sql);
$q="ERROR";
while($row = mysql_fetch_array($query)) {
$query="DELETE FROM npitz_reduced_tmp WHERE
destinazione='".$row['dest']."' AND
ndc LIKE CONCAT('".$row['n']."','%') AND
ndc NOT LIKE '".$row['n']."'";
if($row['n']='77') $q=$query." - ".$row['n'];
mysql_query($query);
}
The variable $row['n'] should contain the result of the SQL query.
After the while loop the variable $q is:
DELETE FROM npitz_reduced_tmp WHERE destinazione='UNITED STATES' AND ndc LIKE CONCAT('','%') AND ndc NOT LIKE '' - 77
The question is: if in the IF statement the value of $row['n'] is '77' why it isn't the same in the $query variable assignment?
You probably should use == instead of = in the if statement
This question already has answers here:
Split a comma-delimited string into an array?
(8 answers)
Closed 10 months ago.
I'm using laravel 5.3 and trying to build a query with multiple where and wherein function. Here is my code:
$posts_id = "1,3,4"; //from my query
$results = Post::select('*');
$results->whereIn('posts_id', [$posts_id]);
$resutls->get();
There is 1 post which have an id of 4 in my database, but my query doesn't returning anything. Though there is 4 in my post ids $posts_id = "1,3,4";.
Need help, to get posts from any of the id, which I've included in my $posts_id variable.
Thanks in Advance!
You need to pass an array to whereIn() method:
$results = Post::whereIn('posts_id', explode(',' $posts_id))->get();
Note that you don't need to call each method in a separate line. You can chain them. Also, the select('*') is not required.
You can create your array in any way you want, no need to use explode().
$ids = [1, 2, 3];
$results = Post::whereIn('posts_id', $ids)->get();
It may be good to check if the array isn't empty before making that query.
This question already has answers here:
How can I loop through a MySQL result set more than once using the mysql_* functions?
(7 answers)
Closed 6 years ago.
So basically I have the following loop to iterate database rows:
while($row = $mysql->fetch_assoc())
But I need to access the rows before this loop as well. So, I do this:
$inside = $mysql->fetch_assoc()
and $mysql loses its rows. When it gets to the while loop it simply does not enter it as the condition becomes NULL.
I tried the following
while($row = $inside)
but this just waits until timeout (loops indefinitely).
Any idea on how I could perform this, making up for the requirements above? Thank you very much for your help...
After you do this :
while ( $row = $mysql->fetch_assoc() )
The internal pointer of $resul is at the end. So you can move it to the beginning again :
$mysql->data_seek( 0 );
while ( $row = $mysql->fetch_assoc() )
All the rows are available again.
Use $mysql->data_seek(0) before to reset the row counter on your second loop iteration. It will allow you to loop through the rows again. See this helpful post for an example.
This question already has answers here:
How can I make SQL case sensitive string comparison on MySQL?
(12 answers)
Closed 6 years ago.
$aa = India::find();
$players = $aa
->where('player LIKE :query', [':query'=>'S%'])
->orderBy('position, player')->all();
$countnumber = $aa->count();
This code returns results without case sensitivity.
Gives results where Player name starts with 'S' or 's'.
But I want to make, just to select Player names only with 'S'.
How to restrict it to select with case sensitivity in PHP Yii2 Framworok ?
In case you are using MySQL you can make your query with BINARY
So your Yii2 code should be something like this:
$players = India::find()
->where(['LIKE BINARY', 'player', $query_parameter])
->orderBy('position, player')->all();
This question already has answers here:
Update the value of a field in database by 1 using codeigniter
(3 answers)
Closed 21 days ago.
I have used this query to increment my column with the selected number like
$this->db->where('my Condition');
$this->db->update('my Table',array('name'=>'gautam','count'=>'2'));
Here I want to add 2 to the actual column value of count. But I can't able to do it with update function.And I cont able to do like
$this->db->update('my Table',array('name'=>'gautam','count'=>'count+2'));
because I'm only getting count of "2" and if I add in my query it is adding ' in my query like
enter code here
Can anyone help me to find out the solution for it.
$this->db->set('name', 'gautam');
$this->db->set('count', 'count+2',FALSE);
$this->db->where('my Condition');
$this->db->update('my Table');
$this->db->set() in Codeigniter
You can use Codeigniter set function to set the value of count with the increment .
From the doc
set() will also accept an optional third parameter ($escape), that
will prevent data from being escaped if set to FALSE. To illustrate
the difference, here is set() used both with and without the escape
parameter.
So you can do something like this :
$this->db->where('my Condition');
$this->db->set('count','count+2',FALSE);//SET COUNT WITH COUNT+2
$this->db->set('name','gautam');
$this->db->update('my Table');
Try this :
$this->db->where('my Condition');
$this->db->set(array('count' => 'count+2', 'name' => 'gautam'), FALSE);
$this->db->update('my Table');