将每一行读入一个数组

时间:2014-02-25 13:53:44

标签: php mysql arrays

我有点卡住,我能够将每一行读入一个数组,但它是一个关联数组,但我不想要那个,我想要一个普通的数组( array = ['2', '3', '4'] )

另外,我的表只有1列,所以应该更容易

这是我的代码。

var_dump给了我:

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

告诉我它是一个相关阵列?

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

3 个答案:

答案 0 :(得分:1)

从行

中取id(第一个元素)
$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;

注意: Please, don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial

答案 1 :(得分: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;
 }

答案 2 :(得分:0)

mysql_query返回索引和关联数组

根据您的需要使用mysql_fetch_array或mysql_fetch_assoc。

哦,你应该使用mysqli函数: - )