It's unrelated to the issue at hand, but you're attempting to utilize parameter sanitizing, which mysql_query does not natively support.
http://www.php.net/mysql_query
Here's a quick and dirty way to achieve what you're looking to do:
PHP Code:
function mysql_prepare( $query, Array $params = array(), $link_identifier = NULL )
{
if ( FALSE === strpos( $query, '?' ) || empty( $params ) )
{
return $query;
}
if ( count( $params ) !== substr_count( $query, '?' ) )
{
throw new InvalidArgumentException(
'Placeholder count does not match parameter count'
);
}
$parts = explode( '?', $query );
// append the first query part
$query = array( array_shift( $parts ) );
foreach ( $parts as $part )
{
// grab the next parameter[s]
$_params = ( array ) array_shift( $params );
// sanitize the parameter[s]
foreach ( $_params as & $_param )
{
if ( isset( $link_identifier ) )
{
$_param = mysql_real_escape_string( $_param, $link_identifier );
}
else
{
$_param = mysql_real_escape_string( $_param );
}
$_param = '\'' . $_param . '\'';
}
// append the parameter[s]
$query[] = implode( ', ', $_params );
// append the next query part
$query[] = $part;
}
return implode( '', $query );
}
Usage:
PHP Code:
$query = mysql_prepare(
'SELECT `title`,`content`,`date` FROM `news` WHERE `id` = ?',
array( $_GET['id'] )
);
$result = mysql_query( $query );
...or, "upgrade" to PDO or MySQLI