I would like to do something like this in Kohana:
$var=someFunction($id);
$q=DB::select(array(DB::expr('table.field-',**$var**),'aliasname'))->from('table')->where('id','=',$id);
What I wanna do is to get a field's value, but a I'd like to subtract a PHP variable from its value.
But the problem is that I cannot execute() the query, because I have to pass it to a method as its argument (without execute), so I can't execute it, and I can't get the value I want from the result array, and subtract my variable from it. I have to do it in MySQL query in some way, and I hope it's possible with DB::expr.
Is that any possible way to do this ?
How about:
$var=someFunction($id);
$q=DB::select(array(DB::expr("(table.field - {$var})"),'aliasname'))->from('table')->where('id','=',$id);
$var = 5; $q = DB::select(array(DB::expr("(table.field - 0)", [$var]),'aliasname'))->from('table')->where('id','=',$id);
Related
In CakePHP3, there is a ORM that helps with building queries.
From the documentation, I can see that
$query = $articles->find(); // build a query that has not run yet
$query->where(['id' => 1]); // Return the same query object
So in this case, I want the string
WHERE `articles`.`id` = 1
After much googling, I found out that there is a way to return just the where clause of a query object.
$query->where(['id' => 1])->clause('where'); // Return the where clause in the form of a QueryExpression
More googling leads me to find out how to get the QueryExpression to spit out string representation
$query->where(['id' => 1])->clause('where')->sql($valueBinder); // Return the where clause in string format
Here is my problem. I don't know what the $valueBinder is supposed to look like. I don't know how to initialize it.
I am also happy not to use ValueBinder as long as I can get the where clause in string format using CakePHP 3 ORM and in the right SQL dialect. Please assume I am using MySQL.
Please advise.
EDIT
I tried to use $query->valueBinder() as the $valueBinder.
It is empty and does not contain the associated c:0 to the value 1.
To directly answer your question, you can get the SQL for any clause this way:
$binder = new \Cake\ORM\ValueBinder();
$query->clause('where')->sql($binder);
That will return the SQL with the correct placeholders, not with the values to be used. The values live in the $binder variable and are used for statement objects.
As I can see, you only wanted to preserve the internal structure of the where clause to pass it to another query in a different request. Your solution is fine, but I'd like to add that you can also encode a full conditions tree from an existing query:
$where = serialize($query->clause('where'));
$anotherQuery->where(unserialize($where)); // A query in another request
In any case, you need to be careful with what you are unserializing as taking it directly from user input will certainly lead to security problems.
You can choose to omit this param if you like. Please see http://api.cakephp.org/3.0/class-Cake.Database.Query.html#_sql
In addition, you can use the Query member function traverse($visitor, $parts) to isolate the where clause. $visitor is a function that takes a value and a clause. You define the behavior of $visitor. $parts is an array of clause names. I suggest passing array('where') into this param.
My workaround is that I store the conditions in json string format.
Using the same example, what I do is
$data['conditions'] = json_encode(['Articles.id' => 1]); // encode into JSON string
$this->DynamicRules->patchEntity($dynamicRule, $data); // use in edit action of DynamicRulesController
then when I need to reuse the conditions, I do:
$articlesTable = TableRegistry::get('Articles');
$query = $articlesTable->find(); // new query for Articles
$rule = json_decode($dynamicRule->conditions, true); // get back the conditions in associative array format
$query->where($rule); // re-assign the conditions back
This got me what I ultimately wanted.
i want to query something from mysql database and put condition that check equality between a value in the table but after round it and other value also after round it how to do this
in php codeigniter ,plz help
i put this instruction put it didn't work
$t=round($latitude,4);
$t1=round($longitude,4);
$this->db->select('place_name');
$this->db->from('place');
$this->db->where(round(`Latitude`,4), $t);
$this->db->where(round(`Longitude`,4),$t1);
$q = $this->db->get();
You need to quote the round call, because right now you're trying to execute a PHP round() function, not the SQL one:
$this->db->where('round(`Latitude`,4)', $t);
^-- ^--
quoting it turns the whole thing into a string, which gets passed into the DB.
I am fetching the values from the column as the value of integer and doing this for two user so i tried to get the value from the table and compare it but unfortunately for both greater and smaller comparsion i am getting the same result nothing changed.
How do i compare the column values?
My code is like below-----
$sqlres="select membership from register where mid='".$_SESSION['mid']."' ";
$pres=mysql_query($sqlres);
$prest=mysql_fetch_array($pres);
$sqlres1="select membership from register where matri_id='".$row['mtr_id']."' ";
$pres1=mysql_query($sqlres);
$prest1=mysql_fetch_array($pres);
if($pres<$pres1)
{
//somethiung enter code here
}
First of all don't use mysql_* it is deprecated, use mysqli_* or PDO instead.
As for your question, mysql_fetch_array as the name suggests, returns an array, not a single value, so you need to get the first value in the array:
if($pres[0]<$pres1[0])
{
//somethiung enter code here
}
You save the result in $prest and $prest1 (with "t"), not in $pres and $pres1. I suggest always using the variable $query for the query string and $result for the result table. Only when you fetch the result should you use a custom variable name to not get confused.
You can use something like
if($prest['membership']<$prest1['membership']){//do stuff here}
In the piece of code that you provided, you are comparing the resources that mysql_query returned not the values of the columns. You have to do this:
if( $prest['membership'] < $prest1['membership'] ){
//Some stuff going here
}
Advice: Name your variables properly. Use more describing names. After 2 months you won't remember the difference between $prest and $prest1
Often I just need to get a single value from MySQL that I know exists there. I use the following construct:
$result = end(mysql_fetch_array(mysql_query('SELECT FOUND_ROWS()', $db)));
Is there a proper single function in PHP that would do this?
Yes, mysql_result will do this.
$result = mysql_result(mysql_query('SELECT FOUND_ROWS()', $db), 0);
I have something similar to the following:
$this->db->set('val', 'val+1', FALSE);
$this->db->where('id', $id);
$query = $this->db->update(TABLE_NAME);
How can I then go, and get the new value of 'val'? Do I have to do another select, or can I get it off of $query?
I've some experience with CodeIgniter and as far as I know you have to requery again.
The update only returns the number of rows affected.
the update query doesnt return anything. if you want to get the value of $val you'll have to run another query
you could run it before running this one, pass the value of val as a parameter to your query next to $id and then return that value + 1.