Jump to content

using variables in the query string


hellonoko

Recommended Posts

I have a feeling im formatting this query string wrong and thats why it is not working.

 

Any ideas?

 

Thanks

 

if ( isset($current_category) )
{
	$query = "SELECT count(*) FROM gallery WHERE category == ".$current_category."";
}
else
{
	$query = "SELECT count(*) FROM gallery";
}

$result = mysql_query($query);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

echo $numrows;
exit();

Link to comment
https://forums.phpfreaks.com/topic/70856-using-variables-in-the-query-string/
Share on other sites

Indeed. Try:

 

$query = "SELECT count(*) FROM gallery WHERE category = '$current_category'";

 

Unlike php, the comparison operator in mysql is a single = sign. Also, whenever you have a problem with a query, try adding an or die statement:

$result = mysql_query($query) or die(mysql_error().'<br />Query was: '.$query);

Hello,

 

You have used '==' in you query for comparison. This might be the cause of your issue. Modify the query

 

$query = "SELECT count(*) FROM gallery WHERE category == ".$current_category."";

 

With the following

 

$query = "SELECT count(*) FROM `gallery` WHERE `category` = '" . $current_category . "'";

 

Hope this will solve your issue.

 

Regards

Hello,

 

hello

this code is correct n work. no need to filter the php variable.

Code:

 

"SELECT count(*) FROM gallery WHERE category == $current_category";

 

]just try this one

 

I just wanted to clarify that my last post was just not to include proper query syntax, but i also pointed out '==' sign in query which is incorrect and will return query error.

I hope i made myself clear.

 

Regards,

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.