Results 1 to 3 of 3

Thread: PHP Array with MySQL

  1. #1
    Join Date
    Dec 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Array with MySQL

    I'm trying to create a Php array from a MySQL Table.
    Here's what I've got:

    Code:
    $LOGIN_INFORMATION = array();
    
    $result = mysql_connect($hostname,$user,$pass);
    mysql_select_db('info_moosehead');
    
    $query = "SELECT admin_name FROM Portal_administrators";
    $result = mysql_query($query);
    
    if( !$result) {
    echo mysql_error() . ": " . mysql_errno();
    }
    
    while ( $row = mysql_fetch_array($result)) {
    	echo $row;
    	array_push($LOGIN_INFORMATION, $row);
    	print_r($LOGIN_INFORMATION);
    	echo "<br>";
    }
    I'm noticing results like this:

    Code:
    ArrayArray ( [0] => Array ( [0] => Airfire [admin_name] => Airfire ) )
    ArrayArray ( [0] => Array ( [0] => Airfire [admin_name] => Airfire ) [1] => Array ( [0] => JAUGER [admin_name] => JAUGER ) )
    ArrayArray ( [0] => Array ( [0] => Airfire [admin_name] => Airfire ) [1] => Array ( [0] => JAUGER [admin_name] => JAUGER ) [2] => Array ( [0] => JBARR [admin_name] => JBARR ) )
    Which isn't what I expected at all, I'm looking for just the row "admin_name" to be pushed into this array, and yet that's what's going into the array.. Why?!

  2. #2
    Join Date
    Dec 2008
    Location
    Fremont, CA
    Posts
    30
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default

    Hi,

    Notice that ,

    PHP Code:
    array_push($LOGIN_INFORMATION$row); 
    $row will only contain array info and not the value itself.

    Heres the code that will help u. I have tested it locally and it works.

    PHP Code:
    <?php

    $LOGIN_INFORMATION 
    = array();

    $result mysql_connect($hostname,$user,$pass);
    mysql_select_db('info_moosehead');

    $query "SELECT admin_name FROM Portal_administrators";
    $result mysql_query($query);

    if( !
    $result) {
    echo 
    mysql_error() . ": " mysql_errno();
    }

    while ( 
    $row mysql_fetch_array($result)) {
        
    array_push($LOGIN_INFORMATION$row['admin_name']);
    }

    $totItems count($LOGIN_INFORMATION); //Contains the total items contained within the array, UPPER BOUND
    $i 0//LOWER BOUND


    while($i<=$totItems){                                      //Iteration through array, you can also use print_r for raw.
    echo $LOGIN_INFORMATION[$i]."<BR>";
    $i++;
    }

    ?>

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Well, admin_name is a column, not a row. If you want to store only that field for each row, then you have to say so:
    Code:
    <?php
    
      mysql_connect($hostname,$user,$pass)
        or die('Connection error.');
      mysql_select_db('info_moosehead')
        or die('Database selection error.');
      $result = mysql_query('SELECT admin_name FROM Portal_administrators')
        or die('Query error:' . mysql_error());
    
      $admins = array();
    
      while ($row = mysql_fetch_array($result))
        $admins[] = $row['admin_name'];
    
      print_r($admins);
    
    ?>
    The extra wrapping array comes from the fact that you add the whole array instead of just the appropriate value from it; the duplication of data with both numerical and string keys is what mysql_fetch_array() does by default. You can override it by passing MYSQL_NUM or MYSQL_ASSOC as the second argument.

    Edit:
    Code:
    $totItems = count($LOGIN_INFORMATION); //Contains the total items contained within the array, UPPER BOUND
    $i = 0; //LOWER BOUND
    
    
    while($i<=$totItems){                                      //Iteration through array, you can also use print_r for raw.
    echo $LOGIN_INFORMATION[$i]."<BR>";
    $i++;
    }
    In other words:
    Code:
    echo implode('<BR>', $LOGIN_INFORMATION) . '<BR>';
    ... but I'm beginning to suspect that $LOGIN_INFORMATION is an inaccurate identifier, in a couple of ways (all-capitals are generally reserved for constants).
    Last edited by Twey; 12-31-2008 at 06:44 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •