Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A bad IF statement - But why??

Status
Not open for further replies.

ruffy

Programmer
Oct 1, 2003
72
US
I thought the IF statement was simple enough,
yet my code keeps slamming into an error
at the bottom of this php code
- and I don't know why.
I hope I'm not too embarrassed when I find out.

<?php
$conn = mysql_connect('localhost:8888', 'root', 'root');
if (!$conn) die('Could not connect: ' . mysql_error());
$db = mysql_select_db("Jokes", $conn);
if (!$db) die ('Can\'t use Jokes database : ' . mysql_error());
...
...
$showPicklist = "<select name= \"picklist\">";
if ($ResAllTables = mysql_query("SHOW TABLES")) {
while ($row = mysql_fetch_row($ResAllTables)) {
array_push($TablesArray, $row[0]);
$showPicklist .= "<option value=\"$row[0]\">$row[0]</option>";
}
$showPicklist .= "</select>";
}
?>

And, if I were to add an echo statement after the IF statement, it too gets flagged for an error.

P.S. This code is part of an INCLUDE file.
 
It would help if you tell us what the error you get is.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Parse error: syntax error, unexpected '}' in /Applications/MAMP/htdocs/my_dbs/phpscripts/my_conn.php on line 10
 
And which one is line 10?

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It points to the } bracket after the while statement.
 
When I comment out all code within the While statement, the same syntax error then points to the } bracket at the end of that IF statement.
 
Well, nothing is standing out. Don't know why its choking there.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
It might be something way before the if, and it just realizes its wrong when it finds the next available closing curly brace.

So what's going on before the If statement, and after the select_db thing.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Looks like it's the array_push statement that's the problem.
 
The array_push function looks o.k. if you remove the
array_push, the error goes away?

As I said, I think you are carrying the error, from before. Its not expecting that closing curly brace there.

Although it should, so I would go back up, and check that any other curly braces before hat one are correctly closed.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Handy little tip: if you have the command-line PHP interpreter installed, you can use the -l (for "lint") option to perform a syntax check on a file. Very useful for catching typos.

Anyway, I ran a syntax check on the code you posted, and it comes up clean. It looks like the problem is either in the section you omitted (the "..." stuff) or carried over from another file when you do the include. Probably a missing semi-colon somewhere.
 
Did you figure this out yet? I get no errors running this code:
Code:
<?php
$conn = mysql_connect('host', 'user', 'pass');
if (!$conn)  die('Could not connect: ' . mysql_error());
$db = mysql_select_db("database", $conn);
if (!$db) die ('Can\'t use Jokes database : ' . mysql_error());
$TablesArray = array() ;
$showPicklist = "<select name= \"picklist\">";
if ($ResAllTables = mysql_query("SHOW TABLES")) {
    while ($row = mysql_fetch_row($ResAllTables))  {
       array_push($TablesArray, $row[0]);
       $showPicklist .= "<option value=\"$row[0]\">$row[0]</option>"; 
    }
    $showPicklist .= "</select>"; 
}

echo $showPicklist ;
?>
*shrug* What version of PHP are you running? I'm running 5.22 on Win-blowz.

Greg
 
Vacunita and AdaHacker; You hit the nail on the head.
It was a missing bracket way outside the code I sent in for inspection.

AdaHacker - How do I determine if I have the command-line PHP interpreter installed?

 
@ruffy

to use the cli, open a terminal window, browse to where you have php installed and type in php -v. this should return the version number of the cli. if you get a blank look from your machine, you probably don't have it installed (which would be unusual).


 
I though it would be. Glad you sorted it out.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top