problems after problems

gamewars

New Member
Messages
16
Reaction score
1
Points
0
Im back with another problem. Same as usual for your help I'll give you mine. I will code in html for you or graphic design for any web site you need help with.


so basicaly whats wrong is that all my info is not showing up on the table. click on the link to see the problem.



http://gamewars.x10hosting.com/availablexbox360wars.php


code


<?php
$con = mysql_connect("localhost","gamewars_gamewars","xxxxxx");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("gamewars_xbox360postedwars", $con);
$result = mysql_query("SELECT * FROM post");
echo "<table border='1'>
<tr>
<th>firstname</th>
<th>lastname</th>
<th>quantity</th>
<th>items</th>
<th>players</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['firstName'] . "</td>";
echo "<td>" . $row['lastName'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['items'] . "</td>";
echo "<td>" . $row['players'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Step 1. Do not post your username/password combination. Edit it out. Better yet, go change the password.

Instead of $row['firstName'] , try $row[0], etc.

Looks like your table columns are not named exactly as you think they are.
That, or there is no info in those cols to begin with. You can browse a table using phpMyAdmin from the cPanel.
 

gamewars

New Member
Messages
16
Reaction score
1
Points
0
Step 1. Do not post your username/password combination. Edit it out. Better yet, go change the password.

Instead of $row['firstName'] , try $row[0], etc.

Looks like your table columns are not named exactly as you think they are.
That, or there is no info in those cols to begin with. You can browse a table using phpMyAdmin from the cPanel.



Thanks a lot email me when ever you need help sorry about the password i did at first then it changed it back for some reason thanks.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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:
// 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:
// 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:
$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.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Change your query to:

PHP:
$result = mysql_query("SELECT firstName, lastName, quantity, items, players  FROM post");

and see what happens.

Also, did you look at the db to make sure those fields actually have values?
 

gamewars

New Member
Messages
16
Reaction score
1
Points
0
yaaaaa i finaly fixed a proble on my own it was a stupid mistake the names were not the samebut thanks for all of your help my web site is done
 
Top