Jump to content

php sql count error


CanMan2004

Recommended Posts

Hi everyone

I have a php query that counts the rows in a table, when I put an if statement in the middle of the query, I get the error

Parse error: syntax error, unexpected T_IF

The code that im using is

[code]if ($search == "679")
{
$sqlresult = mysql_query("SELECT * FROM information WHERE".

if ($nameid != "")
{
" nameid = $nameid AND".
}
" hireit='$hire'", $connection);
$num_rows = mysql_num_rows($sqlresult);
$results = $num_rows;[/code]

The error shows on line

if ($nameid != "")

Can anyone help?

Thanks

Dave
Link to comment
Share on other sites

You are using an if statement after a line which isn't finished and so PHP is returning the T_IF error message. To solve this do something like this in stead:
[code]if ($search == "679")
{
    $sql = SELECT * FROM information WHERE";

    if (!empty($nameid))
    {
        $sql .= " nameid = $nameid AND";
    }

    $sql .= " hireit='$hire'";

    $sqlresult = mysql_query($sql, $connection) or die("Query error : " . mysql_error() . "<br />" . $sql);

    $num_rows = mysql_num_rows($sqlresult);
    $results = $num_rows;[/code]

Also I wuld recommend you to indent your code too (like above).
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.