-3

当我做

SELECT count(*) FROM table_name WHERE id= 2

代码有效,但是当我这样写时:

$try= 2;
SELECT count(*) FROM table_name WHERE id= $try; 

代码不再起作用。有人可以解释一下吗?

4

3 回答 3

2

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.

于 2012-09-11T10:38:57.857 回答
1

MySQL@对变量使用符号。

阅读http://dev.mysql.com/doc/refman/5.0/en/user-variables.html了解更多信息

试试这个会起作用:

SET @try='test';
SELECT count(*) FROM tble_name WHERE id = @try;
于 2012-09-11T10:40:57.217 回答
0

use this

$try= 2;
$sql=sprintf("SELECT count(*) FROM table_name WHERE id=%d",$try); 
于 2012-09-11T10:38:49.727 回答