Joe
Joe

Reputation: 275

Reading each row into an array

Im a bit stuck, I am able to read each row into an array but it is an associative array but I don't want that, i want a normal array ( array = ['2', '3', '4'] )

also, my table only has 1 column, so it should be easier

here is my code.

var_dump gives me :

 array(3) { [0]=> array(1) { [0]=> string(44)      
 "0Av5k2xcMXwlmdEV6NXRZZnJXS2s4T3pSNzViREN6dHc" } [1]=> array(1) { [0]=> string(44) 
 "0Av5k2xcMXwlmdDhTV2NxbXpqTmFyTUNxS0VkalZTSnc" } [2]=> array(1) { [0]=> string(44)   
 "0Av5k2xcMXwlmdDdhdVpMenBTZTltY2VwSXE0NnNmWWc" } } 

which tells me it is an assosiative array?

 $fileList = getLiveList();
    var_dump($fileList);

function getLiveList(){
    $query = "SELECT id FROM livelist";
    $result = mysql_query($query); // This line executes the MySQL query that you typed above

    $array = []; // make a new array to hold all your data

    $index = 0;
    while($row = mysql_fetch_row($result)) // loop to give you the data in an associative array so you can use it however.
    {
         $array[$index] = $row;
         $index++;
    }
    return $array;
}

Upvotes: 0

Views: 162

Answers (3)

Rikesh
Rikesh

Reputation: 26451

Just take id (first element) from row,

$array = []; // make a new array to hold all your data
while($row = mysql_fetch_row($result)) // loop to give you the data in an associative array so you can use it however.
{
     $array[] = $row[0];
}
return $array;

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Upvotes: 1

syl.fabre
syl.fabre

Reputation: 746

$fileList = getLiveList();
var_dump($fileList);

function getLiveList(){
    $query = "SELECT id FROM livelist";
    $result = mysql_query($query); // This line executes the MySQL query that you typed above

    $array = []; // make a new array to hold all your data

   while($row = mysql_fetch_row($result)) // loop to give you the data in an associative array so you can use it however.
   {
       $array[] = $row[0];
   }
   return $array;
 }

Upvotes: 1

Thomas Laier Pedersen
Thomas Laier Pedersen

Reputation: 449

mysql_query return both indexed and associate arrays

use mysql_fetch_array or mysql_fetch_assoc based on your needs.

oh, and you should use mysqli functions instead :-)

Upvotes: 0

Related Questions