MySQL Forums
Forum List  »  PHP

Re: Using the NOT IN function
Posted by: Bill Karwin
Date: April 27, 2006 10:40AM

My first guess is that you're using MySQL 4.0, which does not support subqueries.

You should always check for errors in your PHP code, for instance:

$new_duty_query = mysql_query("SELECT ...");
if (!$new_duty_query) {
die("Error in database query: " . mysql_error());
}

Checking for errors and printing the error message is a crucial part of debugging a web app.

Here's a rewrite of your query that gives the same result but does not use subqueries:

SELECT duty_id, duty_description FROM duties d LEFT OUTER JOIN duties_to_projects p ON d.duty_id = p.duty_id AND p.project_id = $project_id WHERE p.duty_id IS NULL

You should also do something to ensure that the $_GET['project_id'] doesn't break your query. I assume project_id is an integer, so what happens if I were to use your web app and enter a request parameter in the URL like:

project_id=banana!

It'll create a syntax error, and your SQL query won't work.

Don't use request parameters in SQL strings without validating them. For instance:

$project_id = intval(0 + $_GET['project_id']);

Forces the $project_id variable to be an integer.

See http://www.php.net/manual/en/function.intval.php

Regards,
Bill K.

Options: ReplyQuote


Subject
Written By
Posted
April 27, 2006 09:16AM
Re: Using the NOT IN function
April 27, 2006 10:40AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.