/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Display a single Row w/ php

I have a MySQL database with its data organized like this…

| [U]fname[/U] | [U]lname[/U] | [U]email[/U] |
| Timmy | Tikes | [email][email protected][/email] |
| Johny | Leaks | [email][email protected][/email] |

… and what I need is a script that I can implement into my site
where when a logged in user goes to ../myacc.php the script will display
their user details that are in the database, and displays their row
according to a session that has their username which was set when they logged in.

Extra Details:

[LIST]

  • [*]

    The session is set as $_SESSION[‘users’]


  • [*]

    The table name is users


  • [/LIST]

    to post a comment
    PHP

    8 Comments(s)

    Copy linkTweet thisAlerts:
    @SrWebDeveloperJul 01.2009 — First you need to add a new field to your users table called "username" which is typically different than their first and last name, otherwise how are you going to find the correct row in the database?

    Once that's done, assuming all usernames are unique:

    [code=php]
    // Adjust MySQL connection settings...
    $username="???";
    $password = "???";
    $hostname = "localhost";
    $database = "users";

    // Connect to MySQL...
    $conn = mysql_connect($hostname, $username, $password) or die("Connecting to MySQL failed");

    mysql_select_db($database, $conn) or die("Selecting MySQL database failed");

    // Run our query, see if session username exists in session field...
    $sql="select fname,lname,email from users where username='{$_SESSION['username']}' limit 1";
    $result=mysql_query($sql,$conn);

    // Parse our results into $data (as an associative array)...
    $data=mysql_fetch_assoc($result);

    // If one row was found in the result set, username exists...
    if ($mysql_num_rows==1) {
    print "Welcome, {$data['fname']} {$data['lname']}, your E-Mail address is {$data['email']}";
    }
    // Otherwise...
    else {
    print "Sorry, the username $username was not found in our database...";
    }

    [/code]


    Get the idea? Change the password and database stuff accordingly, and adjust the rest cosmetically as you see fit. On a side note I like to wrap {} around associative arrays in my source since it is syntaxically correct to use single quotes around key names, and avoid parsing errors. This method is much easier to write and read than: "blah".$data['keyname']."blah...";

    -jim
    Copy linkTweet thisAlerts:
    @SrWebDeveloperJul 01.2009 — Correction: replace "$mysql_num_rows" with "mysql_num_rows($result)"

    I have no editing powers on this forum, typed that in a little too fast!
    Copy linkTweet thisAlerts:
    @SrWebDeveloperJul 01.2009 — Minor correction: make $username in the last print statement [COLOR=#000000][COLOR=#dd0000]{$_SESSION['username']}[/COLOR][/COLOR]
    Copy linkTweet thisAlerts:
    @cbVisionJul 01.2009 — You could use "email" as the unique field. Every email address is indeed unique, so you wouldn't need to add the username field.
    Copy linkTweet thisAlerts:
    @SrWebDeveloperJul 01.2009 — You could use "email" as the unique field. Every email address is indeed unique, so you wouldn't need to add the username field.[/quote]

    Sure, so long as the OP adjusts the login form to use the email address, which actually is very popular these days instead of username.
    Copy linkTweet thisAlerts:
    @HHCOwnerauthorJul 01.2009 — Everything... Sort of works. :p

    The only thing I can't get to work is that when I go to the page, all the information is blank. Any ideas? ?
    Copy linkTweet thisAlerts:
    @HHCOwnerauthorJul 02.2009 — Never mind... I just forgot to make one of the corrections you said SrWebDeveloper. :rolleyes:

    Anyway, thanks for your help, I got the script working very nicely. ?

  • - Josh
  • Copy linkTweet thisAlerts:
    @SrWebDeveloperJul 02.2009 — Enjoy.
    ×

    Success!

    Help @HHCOwner spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...