Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2008
    Posts
    70
    Thanks
    9
    Thanked 0 Times in 0 Posts

    How do i assign a database record to a variable?

    Hi, how do i assign a database record to a variable in php?

    after accessing mySQL and the table and taking in the result...

    $result = mysql_query("SELECT * FROM user WHERE username='$bang'");

    after this statement what do i do?
    something like this?

    while($row = mysql_fetch_array($result))
    {
    $sang = $row['username'];
    $tang = $row['picture'];
    }



    I'm trying to use the include function in a member list data page. So each time a visitor visits with the username chosen, it will display a picture and the name of the person.

  • #2
    Regular Coder
    Join Date
    May 2008
    Location
    Ohio
    Posts
    231
    Thanks
    3
    Thanked 21 Times in 21 Posts
    If you're only getting one row from the database (you can ensure this by adding "LIMIT 1" to the end of your query), then you don't have to put it in a while loop. Instead just:
    Code:
    $result = mysql_query("SELECT * FROM user WHERE username='$bang'");
    
    $row = mysql_fetch_array($result);
    $sang = $row['username'];
    $tang = $row['picture'];
    Just make sure that if the value $bang is set by the user that you escape any illegal characters! This looks like a potential exploit! Before using $bang in your query use the php function addslashes or mysql_real_escape_string on it.

  • Users who have thanked derzok for this post:

    crays (05-17-2008)

  • #3
    New Coder
    Join Date
    May 2008
    Posts
    70
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Oh, thanks for the info. Ah, i'll do some research on mysql_real_escape_string.

    Thanks for the information, really useful~


  •  

    Posting Permissions

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