CanMan2004 Posted May 13, 2006 Share Posted May 13, 2006 Hi everyoneI 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 errorParse error: syntax error, unexpected T_IFThe 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 lineif ($nameid != "")Can anyone help?ThanksDave Quote Link to comment Share on other sites More sharing options...
448191 Posted May 13, 2006 Share Posted May 13, 2006 You can't use operators where strings are expected.Build a string first, then do the query. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 13, 2006 Share Posted May 13, 2006 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). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.