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

时间:2012-10-12 00:59:47

标签: 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

1 个答案:

答案 0 :(得分:3)

来自PHP Docs on mysql_query

  

mysql_query()向与指定link_identifier关联的服务器上的当前活动数据库发送唯一查询(不支持多个查询)。

如何克服这个?请改用mysqli extension