当我做
SELECT count(*) FROM table_name WHERE id= 2
代码有效,但是当我这样写时:
$try= 2;
SELECT count(*) FROM table_name WHERE id= $try;
代码不再起作用。有人可以解释一下吗?
First of all you have to "include" your parameter properly into string
'SELECT count(*) FROM tble_name WHERE id='.$try;
Second, you have to pass it to mysqli object
$result = $mysqli->query('SELECT count(*) FROM tble_name WHERE id='.$try);
Then you have to fetch result
while ($row = $result->fetch_row()) {
/* your logic here */
}
Obviously you have to create new mysqli
object properly, as explained into link that I've provided you.
MySQL@
对变量使用符号。
阅读http://dev.mysql.com/doc/refman/5.0/en/user-variables.html了解更多信息
试试这个会起作用:
SET @try='test';
SELECT count(*) FROM tble_name WHERE id = @try;
use this
$try= 2;
$sql=sprintf("SELECT count(*) FROM table_name WHERE id=%d",$try);