0

Im using:

$query = "SELECT * FROM mydb WHERE condition = New ORDER BY id ASC";

but i get this error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mydb.php on line 84

however if i remove the where clause it works perfectly, can anyone point me in the right direction?

Is the Where clause not usable when doing a fetch array? Thanks for any help.

edit: error message I've 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 'condition = 'New' ORDER BY id ASC'

2
  • Maybe you need to write $query = "SELECT * FROM mydb WHERE condition LIKE 'New' ORDER BY id ASC"; LIKE comparisons are always case-insensitive. With the backticks around condition of course. Commented Sep 21, 2011 at 10:14
  • spent some more time in learning mysql queries Commented Sep 21, 2011 at 10:24

7 Answers 7

1

always run all your queries this way (at least until you adopt some intelligent lib for this)

$query  = "SELECT * FROM mydb WHERE condition = New ORDER BY id ASC";
$result = mysql_query($query) or trigger_error(mysql_error()." in ".$query);

just because not a single soul in the world can tell what's wrong with your query, but database itself. So, you have to ask it if there were any trouble. Not stackoverflow community (they have no idea anyway) but your db server. That's the point.

Note that you have to be able to watch errors occurred, either on-screen or in the error log.

After getting error message about syntax error you have to check syntax of the displayed query. If there are no visible errors, refer to http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html in case there are reserved word unescaped in your query. condition seems is. So

$query  = "SELECT * FROM mydb WHERE `condition` = New ORDER BY id ASC";

will be solution

6
  • i get: Notice: 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 'condition = 'New' ORDER BY id ASC' Commented Sep 21, 2011 at 8:36
  • thanks for your help but i still receive the same error message after making those changes. Commented Sep 21, 2011 at 8:42
  • are you sure you have added backticks, not apostrophes? Backtick symbol usually sharing the key with ~ on the keyboard Commented Sep 21, 2011 at 8:44
  • Yes i have doubled checked and even tried copying and pasting your example..I'm stumped! Commented Sep 21, 2011 at 8:46
  • check if you're running the actual file you edited. I've just run this query and it returned no errors (save for the absent mydb table of course) Commented Sep 21, 2011 at 8:54
0

You appear to be missing quotes around the word "New".

$query = "SELECT * FROM mydb WHERE condition = 'New' ORDER BY id ASC";

Also, are you passing $query to mysql_fetch_array, or did you just not mention the mysql_query call in your question?

1
  • it doesnt work with quotes hence i took them out as a test more than anything. I didnt mention the query call. Commented Sep 21, 2011 at 8:23
0

Since you have tried adding single quotes to the ('New'),

  1. kindly ensure that the condition is a column in the table you are querying and

  2. that mydb is a table in your database (and not your database name)!

0

You have to quote the string.

$query = "SELECT * FROM mydb WHERE `condition` = 'New' ORDER BY id ASC";

Edit:

condition is a reserved word.

1
  • Try to use the backticks and quotes Commented Sep 21, 2011 at 8:45
-1

Is New one of your columns or just a value? Try this:

$query = "SELECT * FROM mydb WHERE condition = 'New' ORDER BY id ASC";
0
-1
$query = "SELECT * FROM mydb WHERE condition = 'New' ORDER BY id ASC";
$result = mysql_query( $query );
while( $row = mysql_fetch_array( $result ) {
    // use $row
}
0
-1

Never assume that a query will work - expect errors and check for them before processing any results.

$query = 'SELECT * FROM `mydb` WHERE `condition` = "New" ORDER BY `id` ASC';
$result = mysql_query( $query );

if( !$result ){
  // Query Failed. You can access the error details with mysql_error()
}elseif( mysql_num_rows( $result )==0 ){
  // Query Returned No Results
}else{
  while( $r = mysql_fetch_assoc( $result ) ){
    // Do whatever you want with the row, which is $r
  }
}
5
  • NEVER output any errors into browser. it is useless for the site user yet will reveal sensitive information to the possible attacker. Commented Sep 21, 2011 at 8:59
  • Agreed. I simply put that there as a temporary measure whilst locally developing/debugging. I do agree that showing errors through the browser on a live server would constitute a security risk. (Answer amended to allow the OP to decide how to handle errors.) Commented Sep 21, 2011 at 9:13
  • That's the problem. That's why PHP codes are as ugly as they are - because of these temporary measures. Why not to write example code which can be used in the production environment as well? Commented Sep 21, 2011 at 9:15
  • Again, I see your point, but I (and obviously alot of other developers) prefer the instant feedback of seeing debugging messages straight in the browser. Often I create a debug function which, when in development mode, dumps to the browser and in production mode to an error file. But, really, we could spend hours taking a small question and trying to educate the OP on every single aspect of a built-from-scratch solution. Commented Sep 21, 2011 at 9:23
  • you can refer to my answer if you need reliable yet safe solution to this problem Commented Sep 21, 2011 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.