Jump to content


Photo

php sql count error


  • Please log in to reply
2 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 13 May 2006 - 09:54 AM

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

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;

The error shows on line

if ($nameid != "")

Can anyone help?

Thanks

Dave

#2 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 13 May 2006 - 10:00 AM

You can't use operators where strings are expected.

Build a string first, then do the query.

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 13 May 2006 - 01:28 PM

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

Also I wuld recommend you to indent your code too (like above).




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users