0

The following code isn't working as expected using mysql_query() function.

$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;

This is working perfect in phpMyAdmin though. Can anyone tell me what's wrong?

I am getting the following error when I try to run it:

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 Answer 1

3

From the 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.

How to overcome this? Use the mysqli extension instead.

Sign up to request clarification or add additional context in comments.

4 Comments

It isn't working as well. I used mysqli but it's giving the following error: Fatal error: Call to a member function fetch_assoc() on a non-object in /home/abc/public_html/test.php on line 43
The mysqli extension is different from the mysql extension in quite a few ways. Please make sure to correctly fetch the data with the mysqli's API.
I used this with no luck: $mysqli->real_query($sql); $res = $mysqli->use_result(); while ($row = $res->fetch_assoc()) { echo " zip = " . $row['zip'] . "\n"; }
Would you mind updating your question to incorporate the modified source?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.