Zaynt Posted February 19, 2009 Share Posted February 19, 2009 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"; } ?> Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted February 19, 2009 Share Posted February 19, 2009 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 Quote Link to comment Share on other sites More sharing options...
Zaynt Posted February 19, 2009 Author Share Posted February 19, 2009 Ahh...I see. Thanks, man 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.