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
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);

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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,

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.