Not a valid MySQL result resource?

First time using PHP (I’ve always used Feel free to insult me)

I’m curious as to why this code gives me this error:

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


<?php $db = mysql_connect("localhost", "root"); mysql_select_db("mydb",$db); $srch = $_POST['search']; $tbl = $_POST['rdoselct']; $result = mysql_query("SELECT * FROM 'dbtbl' WHERE 1 AND '%$tbl%' LIKE '%$srch%'",$db); echo "n"; while ($row = mysql_fetch_row($result)) { printf("n", $row[1], $row[2], $row[3], $row[4]); } echo "
%s %s,%s,%s
n"; ?>[/code]

Here’s the HTML for the form.




Screen Name


I’ve managed to write 4 seperate search pages (one for each category I want to search in) and use 4 seperate forms consisting of a text input and a submit button. The only major difference between the two methods is my SQL query. It’s

$result = mysql_query(“SELECT * FROM dbtbl WHERE 1 AND txt_city LIKE ‘%$txtcity%’”,$db);
rather than the one I posted above.

Many thanks to anyone who can point me in the right direction.

Your sure the SQL query works? try adding mysql_error() to you page

$result = mysql_query(“SELECT * FROM ‘dbtbl’ WHERE 1 AND ‘%$tbl%’ LIKE ‘%$srch%’”,$db);
if (!$result)
echo "Error: Query Error Description - " . mysql_error();

Hmm…That seems to be the problem

Error: Query Error Description - 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 ''dbtbl' WHERE 1 AND '%txt_city%' LIKE '%Los Angeles%'' at line 1
(Los Angeles is the text I searched for. )

I don’t get an error in my other file where the only difference is I don’t have the $tbl variable in there. Any idea why this could be? I’m no SQL pro, but the query looks correct to me either way.

FROM ‘dbtbl’ WHERE 1 is not a valid sql command. Should be something like FROM ‘dbtbl’ WHERE id =‘1’.

What exactly is 1 referring to?

I don’t know why, but just by removing changing ‘%$tbl%’ to $tbl it all works.

Thanks for the help everyone.

In SQL, table names are not supposed to be enclosed in quotation marks. Quotation marks are used around literal values, like a string to insert or search for.

You can use a backtick mark around a table name (a backtick is this: `, created with the same key as the tilde ~) to mark it as such, but it’s not required unless the table name is also a SQL reserved word.

Sponsor our Newsletter | Privacy Policy | Terms of Service