Jump to content


Photo

Reading user data from SQL and returning it


  • Please log in to reply
3 replies to this topic

#1 lamajlooc

lamajlooc
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 21 October 2006 - 01:24 AM

Hi,
I'm trying to write a script where after loggin in, some of the stored data of a user is read from a SQL table and then printed to their page.  So it has to be dynamic to whoever is logged in.  I'm new at this so any help would be greatly appreciated.

The query I have is:
$query = "SELECT username, daily_score, agg_score, av_score, winner, times_won, win_circle, date_last_in FROM users WHERE username = $_SESSION['username']";

I guess it's a problem with the qotes...but I've tried different combos with escaping and I can't figure it out.  Of course, maybe how I've written it isn't allowed?

Thanks in advance.

#2 Jocka

Jocka
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationDallas, Texas

Posted 21 October 2006 - 01:26 AM

change that last part to this:
username = '" . $_SESSION['username'] . "'

That should get it.. asumming the user is logged in of course.

#3 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 21 October 2006 - 01:36 AM

or, if you have a lot of arrays and don't feel like escaping, you can always enclose array names inside braces:

$query = "SELECT username, daily_score, agg_score, av_score, winner, times_won, win_circle, date_last_in FROM users WHERE username = '{$_SESSION['username']}'";

always good practice to keep any data that the user could potentially edit in single quotes.

#4 lamajlooc

lamajlooc
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 22 October 2006 - 12:28 AM

Thanks guys, that helped, but I have one quick question.  When I read the data from the DB, it seems like the only data that's being read is the first two table rows (first_name) and (username).  Have any idea why that might be?  Also, I use a while loop to print out all the data.  I've used this same code before and it worked fine but for some reason now it seems to not print anyhting.  I don't get any errors when I run the code either.

Here is the script:
<?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 first_name, username, daily_score, agg_score, av_score, winner, times_won, win_circle, date_last_in FROM users WHERE username = '{$_SESSION['username']}'";
    $result = @mysql_query($query);
    $bg = '#eeeeee';  // This is so that each iteration of the while has a different background color.
    echo "Welcome {$_SESSION['first_name']}";
    print_r($_SESSION); //This only prints the first_name and username nothing else
    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {  //This does not return any errors, but also does not return anything.
        $bg = ($bg == '#eeeeee' ? '#ffffff' : '#eeeeee');
        echo '<span style="background:', $bg, ';">', stripslashes($row[0]), '</span><br />';
    }
    mysql_free_result($result);
    mysql_close();
    include_once('../includes/footer.html');
?>

Thanks again for your help.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users