Log in

View Full Version : PHP Array with MySQL



The Midnighter
12-31-2008, 06:01 PM
I'm trying to create a Php array from a MySQL Table.
Here's what I've got:



$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:



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?!

l_kris06
12-31-2008, 06:19 PM
Hi,

Notice that ,


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

$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++;
}

?>

Twey
12-31-2008, 06:26 PM
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:
<?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 (http://www.php.net/mysql-fetch-array)() does by default. You can override it by passing MYSQL_NUM or MYSQL_ASSOC as the second argument.




$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:
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).