2014-03-26 55 views
-2

抱歉,如果我的标题不好。在php中删除数据从数组中的sql查询

我的问题是从数据中的数据从sql查询。

我的SQL查询:

$num = mysql_query("SELECT `id_product_attribute`,`id_product`,`reference` FROM `ps_product_attribute`") 

我用这个代码retreive数据:

$nameArray = array(); 

while($row = mysql_fetch_array($num)) { 
    $nameArray[] = $row; 
} 

echo '<pre>'; 
print_r($nameArray); 
echo '</pre>'; 

输出看起来是这样的:

Array 
(
    [0] => Array 
     (
      [0] => 45 
      [id_product_attribute] => 45 
      [1] => 10640 
      [id_product] => 10640 
      [2] => 1041-0567041700116 
      [reference] => 1041-0567041700116 
     ) 

    [1] => Array 
     (
      [0] => 46 
      [id_product_attribute] => 46 
      [1] => 10640 
      [id_product] => 10640 
      [2] => 1041-0567041700318 
      [reference] => 1041-0567041700318 
     ) etc. 

,但我想这样的输出:

Array 
(
    [0] => Array 
     (
      [0] => 45 
      [1] => 10640 
      [2] => 1041-0567041700116 
     ) 

    [1] => Array 
     (
      [0] => 46 
      [1] => 10640 
      [2] => 1041-0567041700318 
     ) etc. 

我需要在我的代码中更改以实现此目标?

如果我使用fetch_assoc那么我会得到相反的结果。

+1

请RTFM。 http://php.net/mysql_fetch_array – deceze

+1

可能的重复[为什么我的CSV输出包含每个列两次?](http://stackoverflow.com/questions/15230470/why-does-my-csv-output-contain-每列 - 两次) –

回答

1

你需要指定结果的类型,否则你会得到两个:

while ($row = mysql_fetch_array($result, MYSQL_NUM)) { 
    ... 
} 

而且,你真的应该切换到另一个适配器(PDO,mysqli的,等等)。

+0

非常感谢。有效!是的,我知道mysql已弃用,我将在稍后更改,再次感谢 – user2989040

0

http://ca1.php.net/manual/en/function.mysql-fetch-row.php

特定的功能之外,你真的应切换到mysqli_类&功能,因为所有的mysql_功能设定在未来的某一时刻被删除。他们还有其他的好处,但是如果你只是用它们作为直接替代品的话,那还不算太多。

0

使用:

$nameArray = array(); 

while($row = mysql_fetch_array($num, MYSQL_NUM)) { 
    $nameArray[] = $row; 
} 

echo '<pre>'; 
print_r($nameArray); 
echo '</pre>'; 
1

mysql_fetch_array接受一个可选的第二个参数。

像这样:

array mysql_fetch_array (resource $result [, int $result_type = MYSQL_BOTH ]) 

传递常数MYSQL_NUM得到你想要的结果。