Lambneck Posted April 11, 2011 Share Posted April 11, 2011 Not really sure what to ask because I don't know what the problem is. Maybe just need a fresh set of eyes to find out whats wrong. I am trying selecting content from a database table but its not working. I am not getting any errors just a blank screen where the content should be displayed. html <table> <tbody> <tr> <th>Topic</th> <th>Name</th> <th>Date</th> </tr> <?php include 'server/forum.php'; ?> </tbody> </table> php <?php require_once('load_data.php'); $con = mysql_connect($db_host, $db_user, $db_pwd); if (!$con) { die('Could not connect to database: ' . mysql_error()); } $dbcon = mysql_select_db($database); if (!$dbcon) { die('Could not select database: ' . mysql_error()); } $sql = "SELECT * FROM $table ORDER BY id"; $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } $row = mysql_fetch_array($result) while($row) { echo '<tr>'; echo '<td class="forumtd"><b>'; echo '<a href="topic.php?id='.$row['id'].'">'.stripslashes(htmlspecialchars($row['topic'])).'</a>'; echo "</b></td>"; echo '<td class="forumtd"><em>'; echo stripslashes(htmlspecialchars($row['name'])); echo "</em></td>"; echo '<td class="forumtd">'; echo date("l M dS, Y", $row['date']); echo "</td>"; echo "</tr>"; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233374-simple-php-mysql-select-not-working/ Share on other sites More sharing options...
dcro2 Posted April 11, 2011 Share Posted April 11, 2011 I think you're getting your code stuck in an infinite loop because this: while($row) will ALWAYS be true since you assigned it outside the loop (unless mysql_fetch_array() fails of course). I think what you meant to do is this: <?php require_once('load_data.php'); $con = mysql_connect($db_host, $db_user, $db_pwd); if (!$con) { die('Could not connect to database: ' . mysql_error()); } $dbcon = mysql_select_db($database); if (!$dbcon) { die('Could not select database: ' . mysql_error()); } $sql = "SELECT * FROM $table ORDER BY id"; $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } while($row = mysql_fetch_array($result)) //while we get a valid row from mysql_fetch_array() { echo '<tr>'; echo '<td class="forumtd"><b>'; echo '<a href="topic.php?id='.$row['id'].'">'.stripslashes(htmlspecialchars($row['topic'])).'</a>'; echo "</b></td>"; echo '<td class="forumtd"><em>'; echo stripslashes(htmlspecialchars($row['name'])); echo "</em></td>"; echo '<td class="forumtd">'; echo date("l M dS, Y", $row['date']); echo "</td>"; echo "</tr>"; } mysql_close($con); ?> Just another unrelated tip, you're checking for errors with SQL queries twice in some places, like here: $result = mysql_query($sql) or die("Error ". mysql_error(). " with query ". $sql); if (!$result) { die("Query to show fields from table failed:".mysql_error()); } If you put or die(blah); after a mysql_query(), the script will just exit with that message if it fails, so your second check ( if(!$result) ) is useless. Quote Link to comment https://forums.phpfreaks.com/topic/233374-simple-php-mysql-select-not-working/#findComment-1200119 Share on other sites More sharing options...
Lambneck Posted April 11, 2011 Author Share Posted April 11, 2011 dcro2 thanks for noticing that duplicate error check. Also I see what you mean about the while loop and I made the adjustment you suggested. Unfortunately the problem persists. No output, no errors. Quote Link to comment https://forums.phpfreaks.com/topic/233374-simple-php-mysql-select-not-working/#findComment-1200134 Share on other sites More sharing options...
dcro2 Posted April 11, 2011 Share Posted April 11, 2011 Hmm, well, mysql should be giving an error if it can't find the table and I don't know what $table is supposed to be, but just to make sure, echo $sql somewhere so you can see what your query actually is. Also make sure you have rows in that table to output. Add this to the top of the script just for good measure: error_reporting(E_ALL); ini_set('display_errors', 1); Quote Link to comment https://forums.phpfreaks.com/topic/233374-simple-php-mysql-select-not-working/#findComment-1200140 Share on other sites More sharing options...
Lambneck Posted April 11, 2011 Author Share Posted April 11, 2011 Ok thank you dcro2! Finally got an error output and it turned out there was a typo. had an underscore where a hyphen should have been. X( Thanks again for the help! Quote Link to comment https://forums.phpfreaks.com/topic/233374-simple-php-mysql-select-not-working/#findComment-1200153 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.