hellonoko Posted September 27, 2007 Share Posted September 27, 2007 I have a feeling im formatting this query string wrong and thats why it is not working. Any ideas? Thanks if ( isset($current_category) ) { $query = "SELECT count(*) FROM gallery WHERE category == ".$current_category.""; } else { $query = "SELECT count(*) FROM gallery"; } $result = mysql_query($query); $query_data = mysql_fetch_row($result); $numrows = $query_data[0]; echo $numrows; exit(); Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/ Share on other sites More sharing options...
GingerRobot Posted September 27, 2007 Share Posted September 27, 2007 Indeed. Try: $query = "SELECT count(*) FROM gallery WHERE category = '$current_category'"; Unlike php, the comparison operator in mysql is a single = sign. Also, whenever you have a problem with a query, try adding an or die statement: $result = mysql_query($query) or die(mysql_error().'<br />Query was: '.$query); Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/#findComment-356212 Share on other sites More sharing options...
localhost1 Posted September 27, 2007 Share Posted September 27, 2007 u can also write the query this way "SELECT count(*) FROM gallery WHERE category == "$current_category"; Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/#findComment-356215 Share on other sites More sharing options...
localhost1 Posted September 27, 2007 Share Posted September 27, 2007 this is also the way of writing the query "SELECT count(*) FROM gallery WHERE category == $current_category"; Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/#findComment-356216 Share on other sites More sharing options...
d.shankar Posted September 27, 2007 Share Posted September 27, 2007 First try to filter the variable $current_category before directly passing to the SQL Query. Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/#findComment-356217 Share on other sites More sharing options...
redarrow Posted September 27, 2007 Share Posted September 27, 2007 where $_POST['what_ever'] Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/#findComment-356220 Share on other sites More sharing options...
hemlata Posted September 27, 2007 Share Posted September 27, 2007 Hello, You have used '==' in you query for comparison. This might be the cause of your issue. Modify the query $query = "SELECT count(*) FROM gallery WHERE category == ".$current_category.""; With the following $query = "SELECT count(*) FROM `gallery` WHERE `category` = '" . $current_category . "'"; Hope this will solve your issue. Regards Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/#findComment-356231 Share on other sites More sharing options...
localhost1 Posted September 27, 2007 Share Posted September 27, 2007 hello this code is correct n work. no need to filter the php variable. "SELECT count(*) FROM gallery WHERE category == $current_category"; ]just try this one Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/#findComment-356242 Share on other sites More sharing options...
hemlata Posted September 27, 2007 Share Posted September 27, 2007 Hello, hello this code is correct n work. no need to filter the php variable. Code: "SELECT count(*) FROM gallery WHERE category == $current_category"; ]just try this one I just wanted to clarify that my last post was just not to include proper query syntax, but i also pointed out '==' sign in query which is incorrect and will return query error. I hope i made myself clear. Regards, Link to comment https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/#findComment-356254 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.