www.webdeveloper.com
Results 1 to 9 of 9

Thread: [RESOLVED] Display a single Row w/ php

  1. #1
    Join Date
    Apr 2008
    Posts
    102

    resolved [RESOLVED] Display a single Row w/ php

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

    | fname | lname | email |
    | Timmy | Tikes | tommytikes@whatever.com |
    | Johny | Leaks | johnyleaks@whatever.com |

    ... 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:
    • The session is set as $_SESSION['users']
    • The table name is users

  2. #2
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    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:

    PHP Code:
    // 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...";

    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

  3. #3
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    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!

  4. #4
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780
    Minor correction: make $username in the last print statement {$_SESSION['username']}

  5. #5
    Join Date
    Sep 2008
    Location
    Akron, OH
    Posts
    1,131
    You could use "email" as the unique field. Every email address is indeed unique, so you wouldn't need to add the username field.

  6. #6
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780

    Thumbs up

    Quote Originally Posted by cbVision View Post
    You could use "email" as the unique field. Every email address is indeed unique, so you wouldn't need to add the username field.
    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.

  7. #7
    Join Date
    Apr 2008
    Posts
    102
    Everything... Sort of works.

    The only thing I can't get to work is that when I go to the page, all the information is blank. Any ideas?

  8. #8
    Join Date
    Apr 2008
    Posts
    102
    Never mind... I just forgot to make one of the corrections you said SrWebDeveloper.

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

    - Josh

  9. #9
    Join Date
    Jul 2009
    Location
    Falls Church, Va.
    Posts
    780

    resolved

    Enjoy.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles