Jump to content

Recommended Posts

I am trying to check to see if a row already exists in a DB and I must be doing something wrong. I am trying to use something like below that uses four AND clauses (1 for each column in the row) to see if there is a match in the DB :

 

$result = mysql_query("SELECT * FROM diffs WHERE id='$this->id' AND name='$name'");
$num = mysql_num_rows($result);
  if ($num >= 1){
    return true;
  } else {
    return false;
  }  

 

If I use the above with only 1 where match [WHERE id=$this->id] it works fine but as soon as I add [AND name=$name], I get a a warning "mysql_num_rows(): supplied argument is not a valid MySQL result resource" I am not sure what I am doing wrong. Any help would be appreciated. thanks 

Link to comment
https://forums.phpfreaks.com/topic/225658-where-and-query-problem/
Share on other sites

Chances are your query is failing.  Temporarily change your query to this to find out what the error and query value is:

$sql = "SELECT * FROM diffs WHERE id='$this->id' AND name='$name'";
echo $sql;
$result = mysql_query($sql) or die(mysql_error());

I used [name=$name] as an example the real 2nd column was [table=$table]

 

The error I got wasn't really helpful:

 

  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 'table='basic'' at line 1.

However, the problem seems to be that one of my columns is named "table" is usage of this term off limits for SQL syntax? When I used the 3rd column [field=$field] and didn't match the table column it seems to work fine.

 

I used [name=$name] as an example the real 2nd column was [table=$table]

 

The error I got wasn't really helpful:

 

  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 'table='basic'' at line 1.

However, the problem seems to be that one of my columns is named "table" is usage of this term off limits for SQL syntax? When I used the 3rd column [field=$field] and didn't match the table column it seems to work fine.

 

That error message is very helpful actually.  Where did that column come from?  There was no mention of it in the first post.

I guess I'll change it to something else, 'tbl', etc.

 

Changing the name is one solution, but it is good practice to use grave accents (backticks) ` `

ideally avoid reserved words AND use grave accents

 

Eventually you may be unlucky enough to come up against another reserved word. Perhaps if the table was built by someone else. If you are used to using grave accents, you are unlikely to encounter any unexpected syntax errors.

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.