SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP4 using Associated Array

    I have an example that comes from a database:
    PHP Code:

        $userSearch
    =array("user_id"=>1,
                        
    "login"=>"j1234",
                        
    "password"=>"test",
                        
    "email"=>"user@domain.com",
                        
    "firstName"=>"joe",
                        
    "lastName"=>"blow");
        } 
    How do I code this correctly in a associative array format similar to above?

    This is what I am doing...
    PHP Code:
    Basic SQL statement
    $sql
    ="SELECT * FROM user 
            WHERE user_id='
    $id'";

    $result=$db->query($sql);


    while ( 
    $row=$result->fetch() ) {

        
    $userSearch=array($row['user_id'],
                
    $row['login'],
                
    $row['password'],
                
    $row['email'],
                
    $row['firstName'],
                
    $row['lastName']);
        } 
    I would like my $userSeach value array to look similar to my first example. How do I code this properly. Its not working out for me. I cant get the pair name and value setup correctly.

    Thanks for your help.
    Last edited by imchaz; Apr 26, 2007 at 11:36.

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,561
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $userSearch = array();
    while ( $row=$result->fetch() ) {
    foreach($row as $key=>$value){
    $userSearch[$key]= $value;
    }
    }

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2003
    Location
    springfield,IL
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kailash Badu,

    Thank you! That was it!!! I tried similar format but couldnt exactly get the syntax correct.

    Thanks again for responding quickly!


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
  •