Jump to content

mysql_num_rows(): supplied argument is not a valid MySQL result resource


Clone512

Recommended Posts

For some reason I get this error from this script (yes, connect.php connects to the database and selects a database, without the where statement, it works fine, yes the field, to exists):

 

<?php

  include("design/header.php");

  require("connect.php");

 

  $user = $_SESSION['user'];

 

  $query = mysql_query("SELECT * FROM inbox WHERE to='$user'");

 

  $messages = mysql_num_rows($query);

 

  if($messages==0) {

  echo "You have no messages.";

  }

  else {

  echo "Test worked.";

  }

 

  include("design/footer.php");

?>

Ok, did that here's the error I got:

 

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 'to='Testusr'' at line 1.

 

But the problem is I don't get what's wrong.

try two things

 

1) set it up like this:

<?php
$sql = "SELECT * FROM inbox WHERE to='$user'";
echo $sql;
$query = mysql_query($sql) or die(mysql_error());

and make sure the echo'd string is what you expect

 

2) it might fix it if you put brackets around your variable, like

<?php
$sql = "SELECT * FROM inbox WHERE to='{$user}'";

http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

 

to is a reserved work, rename your column to something else or put mysql specific back-ticks around it if you never expect to use your code on a different sql database.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.