SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getting a mysql row into an array

    any ideas on how to do this?
    i am stumped and not too experienced with mysql
    my table is called accounts and looks something like
    username | password | details

    i want to call a row based on if the username matches a variable.
    any help?

  2. #2
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:

    $query 
    "SELECT * from accounts where username like '$username'";
    $doit mysql_query($query);

    while (
    $row=mysql_fetch_array($doit)) {

    $username $row["username"];
    $password $row["password"];
    $details $row["details"];

    echo(
    "$username$password$details");


    I swear to drunk I'm not God.
    Matt's debating is not a crime
    Hint: Don't buy a stupid dwarf Clicky

  3. #3
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just select your row and then use mysql_fetch_array(), for example:

    PHP Code:
    $sql mysql_query("SELECT * FROM table WHERE whatever = '$whatever'") or die(mysql_error());

    $result mysql_fetch_array($sql); 
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  4. #4
    SitePoint Wizard Defender1's Avatar
    Join Date
    Apr 2001
    Location
    My Computer
    Posts
    2,808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just to elaborate a little on this, $result is now an array with the column names as it's elements.

    so you can do $result[username], and use it as a variable.
    Defender's Designs
    I'm Getting Married!

    Not-so-patiently awaiting Harry Potter Book 7 *sigh*

  5. #5
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <?
    // get $userid and $passwordid from form
    $HTTP_POST_VARS[ "userid" ];
    $HTTP_POST_VARS[ "passwordid" ];
    {
    //connect to database
    @ $db = mysql_pconnect( "localhost", "***********" );
    // select database
    mysql_select_db( "blah" );
    // if no connect to database echo error
    if ( !$db){ echo "error"; };
    $sql = mysql_query("SELECT * FROM accounts WHERE username = '$userid'") or die(mysql_error());
    $result = mysql_fetch_array($sql);
    echo("Welcome $result[username]");
    ?>


    ok its giving me a parse error on 14/15 if i take the echo off
    any ideas?

  6. #6
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i mean its giving it on line 15 with the echo in line 14 with it off
    ie summit is wrong somewhere and i dont know where

  7. #7
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got rid of the { on line 4
    still getting a parse error on line 13 now tho

  8. #8
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo("Welcome $result[username]");
    thats sorted it
    but its just saying welcome now
    and not welcome blah
    when i log in with the correct id

  9. #9
    Wibblesticks Gryff's Avatar
    Join Date
    Oct 2001
    Location
    Asgard
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $sql = mysql_query("SELECT * FROM accounts WHERE username = '$userid'") or die(mysql_error());
    $result = mysql_fetch_array($sql);
    $userloggedid = "$result[0]";
    echo("Welcome $userloggedid");


    thats working now
    w00t w00t

  10. #10
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here let me help you along a little further.


    PHP Code:
    $sql mysql_query("SELECT * FROM accounts WHERE username = '$userid'") or die(mysql_error()); 
    $result mysql_fetch_array($sql); 
    extract($result);
    echo(
    "Welcome $username"); 

    http://www.php.net/extract
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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