What can you deduce from the behavior? The "while" loop block is guaranteed to print HTML table row and cell elements, even if $row['firstName'] & al. aren't defined. That no HTML <tr> or <td> are printed tells you that $result is empty. Either the query is returning an empty result or there's an error. You'll need to add error checks to figure out which. This is one reason why you should always include error handling code in your scripts. You could use the outdated "or die (mysql_error(...))" while testing, but you'll need to change it for the production version, so you might as well do it the right way from the get-go.
First, move the db connection code to a different script (that link is particularly important for you to read). The more files hold your password, the more opportunities there are for someone to steal it. Also, if you ever change your password, it's much easier to do if it's only used in a single file.
Next, let's refactor the code that prints the table into a function so that you can use it in other scripts. Here's a simplistic gridView.php to start with:
PHP Code:
// gridView.php
function gridView($rslt, $fields) {
if ($rslt->rowCount()) {
echo '<table><thead><tr>', implode('</th><th>', $fields), '</tr></thead><tbody>';
$parity=False;
while ($row = $rslt->fetch(PDO::FETCH_NUM)) {
// 'parity' class is so you can style even & odd rows differently
echo '<tr class="',($parity ? 'odd' : 'even'),'"><td>', implode('</td><td>', $row) , '</td></tr>';
$parity = !$parity;
}
echo '</tbody></table>';
} else {
echo '<p>No results.</p>';
}
}
Now an error logging/printing function.
PHP Code:
// printError.php
function printError($exc) {
global $dbg, $devEmail;
if ($dbg) {
echo "<pre>$exc</pre>";
} else {
echo "Something went wrong on our end. An error was logged and we'll look into it.";
error_log($msg);
// if you want to be e-mailed a copy of the error, define $devEmail and uncomment the following
#error_log($msg, 1, $devEmail);
}
}
Tying it all together: let's stop using the old MySQL driver (which is horribly outdated) and switch to PDO. Among the many benefits is easier error handling via exceptions.
PHP Code:
$dbg = 1;
// localDB.php defines local_db_connect
include_once('localDB.php');
include_once('gridView.php');
include_once('printError.php');
$fields = array('firstName', 'lastName', 'quantity', 'items', 'players');
$db = local_db_connect('PDO');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
$posts = $db->query("SELECT `" . implode("`, `", $fields) . "` FROM post");
gridView($posts, $fields);
} catch (PDOException $exc) {
printError($exc);
}
Some posting tips: use proper punctuation. Remember, Mr. Period is "your friend at the end." Use whichever of [php], [html] or [code] tags are appropriate around your source code to make it easier to read. They will preserve formatting, separate the code from the rest of your post and (for [php] and [html]) colorize the source, making it easier for people to parse.