nbbcj Posted May 15, 2012 Share Posted May 15, 2012 hi all me again i have this bit of code mysql_connect($db_hostname,$db_username,$db_password); @mysql_select_db($db_database) or die( "Unable to select database"); $whereClauses = array(); if(isset($_GET['bi'])) { $whereClauses[] = "bi=1"; } if(isset($_GET['print'])) { $whereClauses[] = "print=1"; } if(isset($_GET['online'])) { $whereClauses[] = "online=1"; } $query = "SELECT * FROM `project` ORDER BY `project`.`position` ASC "; if(count($whereClauses)) { $query .= " WHERE " . implode(" AND ", $whereClauses); } with this error msg There was a problem with the SQL query: 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 'WHERE online=1' at line 3 I get this kinda error a lot as im still learning is there any way to better find out whats going wrong i use error_reporting(E_ALL); ini_set('display_errors', '1'); at the start of my scripts but it dont tell me a lot. i can work out it some thing to do with the $whereClauses as the page displays ok on show all. Im bit stumped here not even sure what to call the prob is it sql query error or php array problem thanks for any help Link to comment https://forums.phpfreaks.com/topic/262578-any-one-no-whats-wrong-here-help-plz/ Share on other sites More sharing options...
The Letter E Posted May 15, 2012 Share Posted May 15, 2012 echo the query after the implode and paste it up for us to see. Also try removing the space in front of the word WHERE "WHERE " . implode(//etc... I'm not 100% on this, but I think your Where clause needs to come before the order by. Link to comment https://forums.phpfreaks.com/topic/262578-any-one-no-whats-wrong-here-help-plz/#findComment-1345728 Share on other sites More sharing options...
The Letter E Posted May 15, 2012 Share Posted May 15, 2012 I would do it this way: $query = "SELECT * FROM `project`"; if(count($whereClauses)) { $query .= " WHERE " . implode(" AND ", $whereClauses); } $query .= " ORDER BY `project`.`position` ASC "; Link to comment https://forums.phpfreaks.com/topic/262578-any-one-no-whats-wrong-here-help-plz/#findComment-1345729 Share on other sites More sharing options...
nbbcj Posted May 15, 2012 Author Share Posted May 15, 2012 echo the query after the implode and paste it up for us to see. Also try removing the space in front of the word WHERE "WHERE " . implode(//etc... I'm not 100% on this, but I think your Where clause needs to come before the order by. yer im thinking that to Link to comment https://forums.phpfreaks.com/topic/262578-any-one-no-whats-wrong-here-help-plz/#findComment-1345730 Share on other sites More sharing options...
nbbcj Posted May 15, 2012 Author Share Posted May 15, 2012 I would do it this way: $query = "SELECT * FROM `project`"; if(count($whereClauses)) { $query .= " WHERE " . implode(" AND ", $whereClauses); } $query .= " ORDER BY `project`.`position` ASC "; YES that did the trick thank you Link to comment https://forums.phpfreaks.com/topic/262578-any-one-no-whats-wrong-here-help-plz/#findComment-1345740 Share on other sites More sharing options...
The Letter E Posted May 15, 2012 Share Posted May 15, 2012 No problem. Link to comment https://forums.phpfreaks.com/topic/262578-any-one-no-whats-wrong-here-help-plz/#findComment-1345742 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.