简体   繁体   English

MySQL num_rows不返回值(SELECT INTO)

[英]MySQL num_rows doesnt return value (SELECT INTO)

When I run this code in PHP the values are returned correctly 当我在PHP中运行此代码时,正确返回值

$result = $mysqli->query("SELECT `id`, `last_key_ID` FROM `customers` WHERE `email` = '{$email}';");

But as soon as I add the INTO-syntax to it, the num_rows returns no value: 但是只要我添加INTO语法,num_rows就不会返回值:

$result = $mysqli->query("SELECT `id`, `last_key_ID` INTO @id, @last_key_id FROM `customers` WHERE `email` = '{$email}';");

The echo just one line after SELECT-INTO code prints nothing SELECT-INTO代码之后的回显只是一行

echo $result->num_rows;

PS: The same SELECT-INTO code seems to run in MySQL-console perfectly, why doesnt it work in PHP? PS:相同的SELECT-INTO代码似乎完全在MySQL控制台中运行,为什么它在PHP中不起作用?

try: 尝试:

$mysqli->query('SET @id := 0');
$mysqli->query('SET @last_key_id := 0');
$result = $mysqli->query("SELECT @id:=id, @last_key_id:=last_key_ID FROM `customers` WHERE `email` = '{$email}';");

Since SELECT ... INTO statement doesn't return data directly (even in MySQL console it will not diplay the result of the SELECT statement and only something like 由于SELECT ... INTO语句不直接返回数据(即使在MySQL控制台中,它也不会显示SELECT语句的结果,只会像

   Query OK, 1 row affected (0.01 sec)

you can't use num_rows function. 你不能使用num_rows函数。

So use echo $result->affected_rows instead. 所以请改用echo $result->affected_rows

MySQL Server doesn't support the SELECT ... INTO TABLE Sybase SQL extension. MySQL Server不支持SELECT ... INTO TABLE Sybase SQL扩展。

Instead, MySQL Server supports the INSERT INTO ... SELECT standard SQL syntax, which is basically the same thing. 相反,MySQL Server支持INSERT INTO ... SELECT标准SQL语法,这基本上是一回事。

Example... 例...

Read more... 阅读更多...

reason is simple, You are assigning simple query output into an session variable into mysql. 原因很简单,您将简单的查询输出分配到mysql的会话变量中。

Assignment is taking place on backend. 分配正在后端进行。 Now you have to fetch that mysql session variable. 现在你必须获取那个mysql会话变量。

Try this query : 试试这个查询:

$result = $mysqli->query("SELECT `id`, `last_key_ID` INTO @id, @last_key_id FROM `customers` WHERE `email` = '{$email}';SELECT @id, @last_key_id;");

Hope this will help you. 希望这会帮助你。

Pradeep posted one answer that seems good enough to make it work. Pradeep发布了一个似乎足以让它发挥作用的答案。 But i still don't see any particular reason of making use of SELECT INTO statement. 但我仍然没有看到使用SELECT INTO语句的任何特殊原因。

I'll say unless you have special reason to use it, it's better to use normal SELECT statement like this 我会说除非你有特殊的理由使用它,否则最好像这样使用普通的SELECT语句

$result = $mysqli->query("SELECT `id`, `last_key_ID` FROM `customers` WHERE `email` = '{$email}';");

And then, use mysql_fetch_assoc to retrieve the data from different columns. 然后,使用mysql_fetch_assoc从不同的列中检索数据。 This will give you slightly better performance and less workload for DB server. 这将为您提供更好的性能和更少的DB服务器工作负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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