Jump to content

any one no whats wrong here ? help plz


nbbcj

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.