mysql_query() 没有按预期工作,但 phpmyadmin 是

标签 mysql

以下代码未使用 mysql_query() 函数按预期工作。

$conn = mysql_connect('localhost','user','pass') or die (mysql_error());

mysql_select_db('db') or die (mysql_error());

$sql = "SET @orig_lat=33.81978250;SET @orig_lon=-118.10641560; SET @dist =10;SELECT Name, Address, City, State, Zip, 3956 *2 * ASIN( SQRT( POWER( SIN( ( @orig_lat - abs( b.lat ) ) * pi( ) /180 /2 ) , 2 ) + COS( @orig_lat * pi( ) /180 ) * COS( abs( b.lat ) * pi( ) /180 ) * POWER( SIN( ( @orig_lon - b.lng) * pi( ) /180 /2 ) , 2 ) ) ) AS distance FROM tblDealer AS b HAVING distance < @dist ORDER BY distance LIMIT 10";

$result = mysql_query($sql, $conn) or die (mysql_error());

$num_rows = mysql_num_rows($result) or die (mysql_error());

echo $num_rows;

这在 phpMyAdmin 中运行得很好。谁能告诉我出了什么问题吗?

当我尝试运行它时,出现以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET @orig_lon=-118.10641560; SET @dist =10;SELECT Name, Address, City, State, Zi' at line 1

最佳答案

来自PHP Docs on mysql_query .

mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

如何克服这个问题?使用mysqli extension相反。

关于mysql_query() 没有按预期工作,但 phpmyadmin 是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12850811/

相关文章:

php - 使用 php 在 varchar 字段中插入带斜杠的查询

mysql - 如何在 MySQL 数据库中使用查询查找重复项

java - for循环遍历不同的数组/Java

php - 使用 PHP 将 mysqli_insert_id 插入单独的表中

php - 使用 php 和 mysql 将图像更新到数据库中

javascript - Ajax获取sql查询结果

php - 检查前一个 MySQL 查询的状态并执行下一个查询

php - 使用 Android Studio、MySQL 和 php 文件,在何处以及如何保存 JSON 响应类型

Php Mysql - 获取之前的值自动增量并将其插入到另一列

php - 在 MySql 中使用 group by 运行总计