V Posted June 25, 2010 Share Posted June 25, 2010 I'm trying to use the AND statement for an object oriented query using mysqli but I get 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 '.0.1 AND post_id = 23' at line 1 The entire code: require_once("functions.php"); $connection = dbConnect(); $_SERVER['REMOTE_ADDR']; $ip = $_SERVER['REMOTE_ADDR']; $sql = "SELECT * FROM unique_views WHERE ip = $ip AND post_id = {$_GET['post']}"; $result = $connection->query($sql) or die(mysqli_error($connection)); if (mysqli_num_rows($result) == 0) { $sql = "INSERT INTO unique_views (ip, post_id) VALUES ('$ip', '$post')"; $result = $connection->query($sql) or die(mysqli_error($connection)); } I first suspected that I'm doing this part if (mysqli_num_rows($result) == 0) wrong :-\ So I tried.. $num_rows= $result->num_rows; if ($num_rows == 0) and I get the same error.. Do you think the AND statement is causing it? Link to comment https://forums.phpfreaks.com/topic/205875-and-statement-for-mysqli/ Share on other sites More sharing options...
Mchl Posted June 25, 2010 Share Posted June 25, 2010 $ip is a string and as such should be queted in your query. $sql = "SELECT * FROM unique_views WHERE ip = '$ip' AND post_id = {$_GET['post']}"; And BTW: putting a $_GET variable straight into your query is asking for trouble. Link to comment https://forums.phpfreaks.com/topic/205875-and-statement-for-mysqli/#findComment-1077292 Share on other sites More sharing options...
V Posted June 25, 2010 Author Share Posted June 25, 2010 Ooh! I had used strings without the quotes before in simpler queries and got no syntax error. I guess I should add quotes fro now on. Thanks! Link to comment https://forums.phpfreaks.com/topic/205875-and-statement-for-mysqli/#findComment-1077297 Share on other sites More sharing options...
Mchl Posted June 25, 2010 Share Posted June 25, 2010 Ooh! I had used strings without the quotes before in simpler queries and got no syntax error. I guess I should add quotes fro now on. Thanks! It is not possible, unless these strings represented numeric values. Link to comment https://forums.phpfreaks.com/topic/205875-and-statement-for-mysqli/#findComment-1077335 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.