Jump to content

Archived

This topic is now archived and is closed to further replies.

lamajlooc

Where's the data in my array? (Reading from an SQL database)

Recommended Posts

Hi,
This should be a simple problem to fix.  What I want to do is read a bunch of user data from an SQL table and print it.  I've checked my syntax on PHP.net, I've used similar code before....I have no idea what is going wrong and I think it's a case of cognitive dissonance.

There are no errors returned upon running the script so I'm kind of at a loss.  Any help would be appreciated, thanks.

Here's the code:
<?php # the user index page
    require_once ('../includes/config.inc');

    $page_title = "Welcome Home";
    include_once ('../includes/header.html');
    require_once ('../not_web/mysql_connect.php');
    $query = "SELECT username, first_name, daily_score, agg_score, av_score, winner, times_won, win_circle, date_last_in FROM users WHERE username = '{$_SESSION['username']}'";
    $result = @mysql_query($query); // Do the query
   
    echo "Welcome {$_SESSION['first_name']}";
    [color=orange]// Here I used both just to check that I wasn't crazy but originally I was using MYSQL_NUM with correct syntax[/color]
    while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
        echo $row[0], '<br />';
        echo $row["first_name"], '<br />';
        echo $row[2], '<br />';
        echo $row[3], '<br />';
        echo $row[4], '<br />';
        echo $row[5], '<br />';
        echo $row[6], '<br />';
        echo $row[7], '<br />';
        echo $row[8], '<br />';
    }
    mysql_free_result($result);
    mysql_close();
    include_once('../includes/footer.html');
?>

Thanks again.

Share this post


Link to post
Share on other sites
Replace this line:

[code]$result = @mysql_query($query); // Do the query[/code]

With this:

[code]$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // Do the query, show problems if any[/code]

That should help.

Share this post


Link to post
Share on other sites
Thanks for your help, but it didn't change much.  I threw in print_r($result); after the query so it looks like this:

$query = "SELECT username, first_name, daily_score, agg_score, av_score, winner, times_won, win_circle, date_last_in FROM users WHERE username = '{$_SESSION['username']}'";
    $result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // Do the query, show problems if any
    print_r($result);
    echo "Welcome {$_SESSION['first_name']}";

Now it outputs "Resource id #5". 

Any ideas? 

Thanks again.

Share this post


Link to post
Share on other sites
[code]
<?php

$query = "SELECT username, first_name, daily_score, agg_score, av_score, winner, times_won, win_circle, date_last_in FROM users WHERE username = '{$_SESSION['username']}'";
$result = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query); // Do the query, show problems if any

while ($row = mysql_fetch_array($result)) {
  echo "{$row['username']}  {$row['first_name']} etc.. ";
}
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.