简体   繁体   中英

How to insert a variable into Kohana DB::expr?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM