Jump to content

Reading user data from SQL and returning it


lamajlooc

Recommended Posts

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.
Link to comment
Share on other sites

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

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

always good practice to keep any data that the user could potentially edit in single quotes.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.