gpt4 book ai didi

php 返回 null 而 mysql 返回用户变量的值

转载 作者:行者123 更新时间:2023-11-29 05:15:55 25 4
gpt4 key购买 nike

以下查询在使用 MySql Workbench 运行时返回“5”,但使用 PHP 时针对相同的本地 apache 服务器返回 null:

SELECT @distance_in_km from branch where (@distance_in_km := 5) limit 1

另一方面,当查询是

SELECT 5 as distance_in_km from branch limit 1

Workbench 和 PHP 都返回 5。

我知道我可以通过如下修改第一个查询来解决它(让字段 distance_in_km 的值为 5 作为查询的结果):

SELECT 5 as distance_in_km, @distance_in_km from branch where (@distance_in_km := 5) limit 1

在这种情况下,workbench 返回“5,5”,PHP 返回“5, null”,这对我来说很好,因为我使用 json 获取值,因此我可以查找字段“distance_in_km”而不是“@distance_in_km”。

但是,我的实际查询要复杂得多,我想避免两次计算字段“distance_in_km”。

编辑 1

问题似乎出在 PHP 计算用户变量“distance_in_km”的方式上。在进一步测试中,我发现尽管 PHP 返回空值,但它确实知道它是真实值,因为在添加 WHERE 测试时,PHP 会正确计算:

SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 4 limit 1

PHP 返回 0 条记录,而将上述测试中的 4 更改为 5,PHP 返回 1 条记录。

另一方面,PHP 在添加 'ORDER BY @distance_in_km' 子句时没有正确排序(它返回按主键排序的记录,这表明 PHP 使用的是 '@distance_in_km' 的空值,而不是它的真正的值(value)。

编辑 2

PHP 5.5.12 版、Apache 2.4.9 版、MYSQL 5.6.17 版(均取自 wampserver 2.5 菜单)。

从同一个菜单中,我打开了 MySQL 控制台,并运行上面的查询并从 PHP 中获得了结果。这是 session

使用控制台:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 476
Server version: 5.6.17 MySQL Community Server (GPL)

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use q;
Database changed
mysql> SELECT @distance_in_km from branch where (@distance_in_km := 5) limit 1;
+-----------------+
| @distance_in_km |
+-----------------+
| NULL |
+-----------------+
1 row in set (0.00 sec)

mysql> SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 4 limit 1;
Empty set (0.00 sec)

mysql> SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 5 limit 1;
+-----------------+
| @distance_in_km |
+-----------------+
| 5 |
+-----------------+
1 row in set (0.00 sec)

mysql>

PHP 代码

$query = "SELECT @distance_in_km from branch where (@distance_in_km := 5) <= 5 limit 1";
$result = $db->query($query) or die('Err: Table not found: ' . mysql_error());
echo json_encode($result->fetch_row());

编辑 3

根据@Stan 建议的修改后的 SELECT 语句,我尝试更改我的代码但没有成功。可以看到 distance_in_km 没有从 branch 获得正确的值。

mysql> SELECT @distance_in_km, latitude from branch, (SELECT @distance_in_km := latitude from branch) as uservar limit 3;
+-----------------+-----------+
| @distance_in_km | latitude |
+-----------------+-----------+
| 32.0616233 | 32.180644 |
| 32.0616233 | 32.195598 |
| 32.0616233 | 32.197176 |
+-----------------+-----------+
3 rows in set (0.00 sec)

编辑 4

SELECT @distance_in_km, co.*, c.*
FROM `branch` c
LEFT JOIN open_hours co ON c.open = co.id,
(SELECT @distance_in_km := 111.1111 * DEGREES(ACOS(COS(RADIANS(latitude)) *
COS(RADIANS('32.177242')) * COS(RADIANS(longitude) - RADIANS('34.863834')) + SIN(RADIANS(latitude)) *
SIN(RADIANS('32.177242')))) from branch) as uservar
WHERE c.company='1' and @distance_in_km <= 20
ORDER BY @distance_in_km LIMIT 5 OFFSET 0;

编辑 5

在出现更好的解决方案之前,我结束了将计算定义为 UDF(用户定义的函数)并且我在同一个 SELECT 中调用它 3 次!!!。效率很低,但我别无选择。因此 SELECT 现在看起来:

SELECT distance_in_km(latitude, 32.177242, longitude, 34.863834) as km, c.latitude, c.longitude, c.id FROM `branch` c
WHERE c.company='1' and distance_in_km(latitude, 32.177242, longitude, 34.863834) <= 20
ORDER BY distance_in_km(latitude, 32.177242, longitude, 34.863834) LIMIT 5 OFFSET 0

在进一步测试中,如果我在 ORDER BY 中使用变量 km 看起来它也有效,这样我在 SELECT 中有两次函数

SELECT distance_in_km(latitude, 32.177242, longitude, 34.863834) as km, c.latitude, c.longitude, c.id FROM `branch` c
WHERE c.company='1' and distance_in_km(latitude, 32.177242, longitude, 34.863834) <= 20
ORDER BY km LIMIT 5 OFFSET 0

最佳答案

您可能应该阅读以下内容:MySQL user variables guide

具体来说:

As a general rule, other than in SET statements, you should never assign a value to a user variable and read the value within the same statement. For example, to increment a variable, this is okay:

SET @a = @a + 1; For other statements, such as SELECT, you might get the results you expect, but this is not guaranteed. In the following statement, you might think that MySQL will evaluate @a first and then do an assignment second:

SELECT @a, @a:=@a+1, ...; However, the order of evaluation for expressions involving user variables is undefined.

简短版本:在您的案例中,变量在查询执行后被绑定(bind),因此它没有任何值(value)。尝试在同一 session 中执行您的查询两次,作为此类“未定义”行为的证据。

更新:如果您真的想绑定(bind)变量并在同一语句中使用它,您可能想尝试从子查询中进行选择,如下所示:

SELECT @distance_in_km from branch, (SELECT @distance_in_km := 5) as uservar limit 1

我认为这是不好的做法,但它确实有效,而且我知道有时您别无选择,所以我想请明智地使用它。

关于php 返回 null 而 mysql 返回用户变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32911163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com