Jump to content

AND statement for mysqli


V

Recommended Posts

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
Share on other sites

$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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.