0

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;
}
1
  • you don't need $index in your while loop: you can juste use $array[] = $row[0] Commented Feb 25, 2014 at 13:56

3 Answers 3

1
$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;
 }
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.