Saturday, April 25, 2009

MySQL - Resolved null issue in query.


We have faced strange issue with MySql old version (version 4).

Our php file will pass a querystring variable "id". It can have a value or it can be empty in some situation.

Initially, we used below code,
$id=$_GET['id'];

$sql="select school_name from school_table where id=$id";
$result=mysql_query($sql);

It was working fine if the $id is assinged with a value.
It showed syntax error if $id is not having any value.

Below workaround resolved the issue.

Added below code above the query.
if ($id=="")
{
$id="null";
}
More Articles...

No comments:

Search This Blog