Jump to content

[SOLVED] can't echo anything after while loop


Zaynt

Recommended Posts

I have a script that returns data from a mysql database. I retrieve  the data with a while loop and present i all in a <table>. The problem is that the script won't write the table end (</table>) after the loop...

 

if ( isset($_GET['Knapp']) ){

$startNr =  $_GET['startNr'];
$antallNr = $_GET['antallNr'];
$postNr = $_GET['postNr'];



$kobling = mysql_connect($dbtjener, $bruker, $passord);


mysql_select_db($database, $kobling) or die("kunne ikke koble til database: ". mysql_error());



$q = mysql_query("SELECT * FROM table WHERE postNR >= $startNr ORDER BY postNr ASC LIMIT $antallNr");


echo"
<h2>Søk etter postnummer:</h2>
<form action=\"\" method=\"get\">
<table border=\"0\">
<tr>

	<td>Første postnummer:</td><td><input type=\"text\" name=\"startNr\" size=\"4\" value=\"$startNr\" /></td>
</tr>
<tr>
	<td>Antall postnummer:</td><td><input type=\"text\" name=\"antallNr\" size=\"4\" value=\"$antallNr\" /></td>
</tr>
<tr>
	<td>Søk postnummer:</td><td><input type=\"text\" name=\"postNr\" size=\"4\" value=\"$postNr\"/></td>

</tr>
</table>
<input type=\"submit\" name=\"Knapp\" value=\"Vis adresser\" />
</form>\n\n";


echo"<p>Resultat:</p>\n";
echo"<table border=\"0\" cellpadding=\"3\">\n\n";
echo"\t<tr>\n";
echo"\t\t<th scope=\"col\">Postnr</th>\n";
echo"\t\t<th scope =\"col\">Poststed</th>\n";
echo"\t</tr>\n\n";


$i = 0;
while($row = mysql_fetch_array($q) or die(mysql_error())) {
$i++;

$farge = ($i%2) ? "odd":"even";

if ($row['postNr'] == $postNr){
	$farge = "hit";
	}



echo "\t<tr class=\"$farge\">\n\t\t<td>"; 
echo $row['postNr'];
echo "</td>\n\t\t<td>"; 
echo $row['postSted'];
echo "</td>\n\t</tr>\n\n"; 

} 
echo "</table>\n";

mysql_close($kobling);


}
else{
echo"
<h2>Søk etter postnummer:</h2>
<form action=\"\" method=\"get\">
<table border=\"0\">
<tr>

	<td>Første postnummer:</td><td><input type=\"text\" name=\"startNr\" size=\"4\" value=\"7290\" /></td>
</tr>
<tr>
	<td>Antall postnummer:</td><td><input type=\"text\" name=\"antallNr\" size=\"4\" value=\"50\" /></td>
</tr>
<tr>
	<td>Søk postnummer:</td><td><input type=\"text\" name=\"postNr\" size=\"4\" value=\"7300\"/></td>

</tr>
</table>
<input type=\"submit\" name=\"Knapp\" value=\"Vis adresser\" />
</form>\n";
}


?>

Start by being sensible about the way you execute your SQL query

 

Instead of

while($row = mysql_fetch_array($q) or die(mysql_error())) {

do

while($row = mysql_fetch_array($q)) {

 

Using or in a condition tests the first condition. If that matches True, it doesn't even bother with the second condition after the or.

However, when the first condition is false, it will test the second to see if that is tru.

In your case, the second isn't a true condition but a statement... you're telling PHP to die(); but there hasn't been any mySql error, so there will be no additional display, the script will simply terminate

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.