calabiyau Posted May 16, 2007 Share Posted May 16, 2007 $query = "SELECT * FROM questions WHERE level='1' AND column='1'"; $query = "SELECT * FROM questions WHERE (level='1') AND (column='1')"; I'm trying to select rows based on values in two columns but it is throwing up NOT A VALID MYSQL RESOURCE error. Can some one help me with the correct syntax to use here? Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/ Share on other sites More sharing options...
mmarif4u Posted May 16, 2007 Share Posted May 16, 2007 Try this: $query = "SELECT * FROM questions WHERE level=1 AND column=1"; Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254107 Share on other sites More sharing options...
calabiyau Posted May 16, 2007 Author Share Posted May 16, 2007 No I had tried that as well. doesn't work. i'm really stumped. i've never had to select data based on two columns before. i've verified that the columns exist in the table, tried various combinations of paranthesis, nothing seems to be working. Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254108 Share on other sites More sharing options...
btherl Posted May 16, 2007 Share Posted May 16, 2007 Please show how you are using the query, especially the call to mysql_query(), as well as any error checking you are doing. Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254109 Share on other sites More sharing options...
calabiyau Posted May 16, 2007 Author Share Posted May 16, 2007 $server=''; $user=''; $password=''; $mydtb=''; //all the connection info is correct for sure //also when i run the query with just WHERE level=1 MINUS THE AND CLAUSE it works correctly $connect=mysql_connect($server,$user,$password); mysql_select_db($mydtb); $query = "SELECT * FROM questions WHERE level=1 AND column=1"; $result = mysql_query( $query, $connect ); while ($row = mysql_fetch_array ($result) ) { echo $row['question']; } Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254110 Share on other sites More sharing options...
calabiyau Posted May 16, 2007 Author Share Posted May 16, 2007 This is so weird. I had a bit of a brain freeze I guess. I've been reusing the same log in system for so long now that I forgot that that was the last time I used this type of query and always I have no problems with it. $query = "SELECT * FROM users WHERE username='".$user."' AND password='".$password."'"; Can anyone think of any reason why it wouldn't be working now? I mean the code I posted is pretty much all there is to it. I literally just started working on this. Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254118 Share on other sites More sharing options...
TEENFRONT Posted May 16, 2007 Share Posted May 16, 2007 Stab in the dark but i always tend to backtick my queries..not sure if this actually matters but try this.. $query = "SELECT * FROM `questions` WHERE `level`='1' AND `column`='1'" Also i take it you defianlty have 2 fields name level and column , no typos? Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254120 Share on other sites More sharing options...
calabiyau Posted May 16, 2007 Author Share Posted May 16, 2007 The table has only four fields: id level column question Just as an idea i tried selecting by level and id, instead of level and column and it worked, so I wonder if perhaps column is a reserved word that has a special function to mysql, thereby breaking the query. Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254121 Share on other sites More sharing options...
TEENFRONT Posted May 16, 2007 Share Posted May 16, 2007 use the back ticks like i suggested..... Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254123 Share on other sites More sharing options...
calabiyau Posted May 16, 2007 Author Share Posted May 16, 2007 Yep that did it. Thanks. I've never really needed to do that in any of my queries before but it solved the problem. Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254125 Share on other sites More sharing options...
TEENFRONT Posted May 16, 2007 Share Posted May 16, 2007 As far as i know it escapes the mysql reserved words, best to do it in most cases to avoid any clashes in names etc. Quote Link to comment https://forums.phpfreaks.com/topic/51593-solved-query-problem-where-with-two-and/#findComment-254162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.