Jump to content


Photo

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


  • Please log in to reply
3 replies to this topic

#1 lamajlooc

lamajlooc
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 October 2006 - 11:54 PM

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']}";
    // Here I used both just to check that I wasn't crazy but originally I was using MYSQL_NUM with correct syntax
    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.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 23 October 2006 - 01:49 AM

Replace this line:

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

With this:

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

That should help.
Legend has it that reading the manual never killed anyone.
My site

#3 lamajlooc

lamajlooc
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 23 October 2006 - 02:05 AM

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.

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 23 October 2006 - 05:21 AM

<?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.. ";
}
?>

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users