...

View Full Version : else stament error



sunadmn
04-07-2004, 08:16 PM
Good day all I have been working for my first time with PHP and have written a short script to connect to my local mySQL DB and have it working ok till I added an else statment. I now recieve the following error:
Parse error: parse error, unexpected T_ELSEIF in /usr/local/apache2/htdocs/dns.php on line 55

Could anyone take a look and tell me what I may be missing here as I am lost.

Thanks
-SUNADMN

code below:

<?

if (isset($_POST['SEARCHDOM'])) { # Start a domain search
$searchdom = strtolower($_POST['SEARCHDOM']);
$database="dns";
$user = "root";
$password = "bla";
$host = "localhost";

$my_db = mysql_connect("localhost", "$user", "$password") or die ("Could not connect to Database!");

$query = "SELECT * from a where zone='$searchdom'";
print "Query is $query<br>";
$result = mysql_db_query("$database", $query);
while( $arr = mysql_fetch_array($result) ) {
$flag=1;
print "<CENTER><H3><FONT COLOR='GREEN'>Domain found: $arr[0].$arr[1]</FONT></H3><BR></CENTER>";
print "<TABLE align='center' width='75%' cellspacing='0' cellpadding='2' border='3' style='font-family: Verdana,A
rial,Helvetica,Sans Serif; font-size: 10pt;font-weight: bold; color: blue;'>";
print "<TR><TD>Hostname</TD><TD>$arr[0]</TD></TR>";
print "<TR><TD>Zone</TD><TD>$arr[1]</TD></TR>";
print "<TR><TD>IP(Numerical)</TD><TD>$arr[2]</TD></TR>";
print "</TABLE>";
print "<P><HR><BR>";
}
else {

$query ="SELECT * from a where host='$searchdom'";
print "Query is $query<br>";
$result = mysql_db_query("$database", $query);
if( $arr = mysql_fetch_array($result) ) {
$flag=1;
print "<CENTER><H3><FONT COLOR='GREEN'>IP found: $arr[0].$arr[1]</FONT></H3><BR></CENTER>";
print "<TABLE align='center' width='75%' cellspacing='0' cellpadding='2' border='3' style='font-family: Verd
ana,Arial,Helvetica,Sans Serif; font-size: 10pt;font-weight: bold; color: blue;'>";
print "<TR><TD>Hostname</TD><TD>$arr[0]</TD></TR>";
print "<TR><TD>Zone</TD><TD>$arr[1]</TD></TR>";
print "<TR><TD>IP(Numerical)</TD><TD>$arr[2]</TD></TR>";
print "</TABLE>";
print "<P><HR><BR>";
}

if ($flag != 1) {

print "<H3><FONT color='RED'>Domain $SEARCHDOM not found!</FONT></H3><P><HR>";

}

}

else {
print "<H3><FONT color='RED'>No Searchdom found....</FONT></H3><P><HR>";
}

?>

Nightfire
04-07-2004, 08:41 PM
Please next time use the
tags. You have an else clause on a while loop, which you can't have. I've tried to understand your code and fix it for you


<?php

if (isset($_POST['SEARCHDOM'])) { // Start a domain search
$searchdom = strtolower($_POST['SEARCHDOM']);
$database="dns";
$user = "root";
$password = "bla";
$host = "localhost";

$my_db = mysql_connect("localhost", "$user", "$password") or die ("Could not connect to Database!");

$query = "SELECT * from a where zone='$searchdom'";
print "Query is $query<br>";
$result = mysql_db_query("$database", $query);
/* FIX HERE TO SEE IF $SEARCHDOM exists */
if(mysql_num_rows($result) >=1){ // result found, continue

while( $arr = mysql_fetch_array($result) ) {
$flag=1;
print "<CENTER><H3><FONT COLOR='GREEN'>Domain found: $arr[0].$arr[1]</FONT></H3><BR></CENTER>";
print "<TABLE align='center' width='75%' cellspacing='0' cellpadding='2' border='3' style='font-family: Verdana,Arial,Helvetica,Sans Serif; font-size: 10pt;font-weight: bold; color: blue;'>";
print "<TR><TD>Hostname</TD><TD>$arr[0]</TD></TR>";
print "<TR><TD>Zone</TD><TD>$arr[1]</TD></TR>";
print "<TR><TD>IP(Numerical)</TD><TD>$arr[2]</TD></TR>";
print "</TABLE>";
print "<P><HR><BR>";
}
}else{ // NO results found, echo error
print "<H3><FONT color='RED'>Domain $SEARCHDOM not found!</FONT></H3><P><HR>";
}
/*
THIS IS DOING EXACTLY THE SAME AS ABOVE, WHY DO YOU HAVE IT?
}else {

$query ="SELECT * from a where host='$searchdom'";
print "Query is $query<br>";
$result = mysql_db_query("$database", $query);
if( $arr = mysql_fetch_array($result) ) {
$flag=1;
print "<CENTER><H3><FONT COLOR='GREEN'>IP found: $arr[0].$arr[1]</FONT></H3><BR></CENTER>";
print "<TABLE align='center' width='75%' cellspacing='0' cellpadding='2' border='3' style='font-family: Verdana,Arial,Helvetica,Sans Serif; font-size: 10pt;font-weight: bold; color: blue;'>";
print "<TR><TD>Hostname</TD><TD>$arr[0]</TD></TR>";
print "<TR><TD>Zone</TD><TD>$arr[1]</TD></TR>";
print "<TR><TD>IP(Numerical)</TD><TD>$arr[2]</TD></TR>";
print "</TABLE>";
print "<P><HR><BR>";
}
*/
/* Reliable fix is above
if ($flag != 1) {
print "<H3><FONT color='RED'>Domain $SEARCHDOM not found!</FONT></H3><P><HR>";
}
*/
}
else {
print "<H3><FONT color='RED'>No Searchdom found....</FONT></H3><P><HR>";
}

?>

btw, try not to use the perl/cgi comments (#) they're depreciated and shouldn't be used anymore.

raf
04-07-2004, 09:03 PM
Please use the php tags. I also see quite some problems and room for optimalisation in your code, like just selecting the sql and then execiting and displaying it for the two cases with the same code


<?php //don't use shorttags
// no need to first dump the userdetails in variables; It's even insecure to not unset them after connectiong ...
//use @ in front of these functions to suppres error and preven that your accountdetails are disclosed that way
$my_db = @mysql_connect('localhost', 'root', 'bla') or die ('Could not connect to Database!');
//you better select the db here. unless you use more then one db in your script
@mysql_select_db('dns',$my_db )
or die ('Could not select database. Please go back and try again or try again later.');
// the connectionstrings should also be placed in an include so that you can reuse them and so that they are not disclosed if the server is falling over and sends unparsed code to the client
if (isset($_POST['SEARCHDOM'])) { # Start a domain search
//never use * --> specify the fields you actuala need, even if you use all current fields of that table
// always concatenate strings and variables
$query = "SELECT * from a where zone='" . $searchdom. "'";
//print "Query is $query<br>";
} else {
$query ="SELECT * from a where host='$searchdom'";
}
// rewrote it and added some errorhandling + using the connectionID
$result = mysql_query($query, $my_db ) or die ('Queryproblem');
/* since you only use the enumerated array, you shoudl beteter use mysql_fetch_row.
but i would realy recommend using the associated array with mysql_fetch_assoc --> much easier to read
you should also check if there are rows returned*/
if (mysql_num_rows($result) >= 1){
while( $arr = mysql_fetch_row($result) ) {
/* oooh. try to use valid html code !!
--> only lower case character + use double quotes for the atributevalues
+ maybe some more css for the layout and use an external stylesheet?
+ again the concatination and then use single quotes for the strings
+ no need to start and end the print for each line
(only edited the first line)*/
print '<center><h3><font color="green">Domain found: ' . $arr[0].$arr[1].'</font></h3></center><br />
<table align="center" width="75%" cellspacing="0" cellpadding="2" border="3" style="font-family: Verdana,A
rial,Helvetica,Sans Serif; font-size: 10pt;font-weight: bold; color: blue;">';
//these should all be change accoringly --> dropping the prints etc
print "<TR><TD>Hostname</TD><TD>$arr[0]</TD></TR>";
print "<TR><TD>Zone</TD><TD>$arr[1]</TD></TR>";
print "<TR><TD>IP(Numerical)</TD><TD>$arr[2]</TD></TR>";
print "</TABLE>";
print "<P><HR><BR>";
}
}elseif (isset($_POST['SEARCHDOM'])) {
//same html probs and concatinations here
print "<H3><FONT color='RED'>Domain $SEARCHDOM not found!</FONT></H3><P><HR>";
}else{
print "<H3><FONT color='RED'>No Searchdom found....</FONT></H3><P><HR>";
}
?>


<edit>hey Nightfire. Once again we crossposts. Seems i'm even pickier then you :D ) </edit>

Nightfire
04-07-2004, 09:07 PM
Ahh, raf got better eyes than me heh. Didn't notice all the little things you did.
<edit>We gotta stop doin this ya know lol</edit>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum