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";
}


?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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