PHP:将查询结果插入数组元素

时间:2010-10-05 05:14:47

标签: php mysql arrays

如何将查询结果分配给数组元素?

这是我的代码:

include('db.php');

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("Database connection error");
mysql_select_db($dbname);
$query = "select * from test where value=20";
$result = mysql_query($query);
$vegetable_list = array('$rice', '$wheat', '$potato', '$pulses');
$i = 1;
while($row_result = mysql_fetch_row($result))
{
    ??????? = $row_result[$i];
    $i++;
}

如何将查询结果分配到数组中?让我们说:

$rice = $row_result[1];
$wheat = $row_result[2];
$potato = $row_result[3];

如何自动分配值?

5 个答案:

答案 0 :(得分:3)

试试吧:

$vegetable_list= array('rice','wheat','potato','pulses'); //no $ symbol
while($row_result=mysql_fetch_row($result))
{
    foreach($vegetable_list as $k => $v)
         ${$v} = $row_result[$k + 1]; //I think mysql_fetch_row should indexing from 0 -> n (not from 1)
}

答案 1 :(得分:3)

好的不确定,但似乎你有一个150列x 20rows表,你想转换成二维数组。就这么简单:

$data = array( );

while( $row = mysql_fetch_assoc( $result ) )
{
    // at this point, $row contains a single row as an associative array
    // keys of this array consist of column names
    // all you need to do is append $row to $data
    $data[ ] = $row;
}

// $data is a two dimensional array
// $data[ 0 ] contains 1st row
// $data[ 1 ] contains 2nd row
// ...
// $data[ 0 ][ 'rice'  ] contains rice column value for 1st row
// $data[ 0 ][ 'wheat' ] contains wheat column value for 1st row
// ...
// $data[ 1 ][ 'rice'  ] contains rice column value for 2nd row
// $data[ 1 ][ 'wheat' ] contains wheat column value for 2nd row
// ...
// and so on

var_dump( $data );

答案 2 :(得分:1)

编辑了代码。

下面:

include('db.php');
$conn=mysql_connect($dbhost,$dbuser,$dbpass) or die("Database connection error");
mysql_select_db($dbname);

$query="select * from test where value=20";
$result=mysql_query($query);
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$vegetable_list= array( array () );
$rcols = mysql_query("SHOW COLUMNS FROM test");
if (!$rcols) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
if (mysql_num_rows($result) > 0) {

    $i = 0;
    $j = 0;
    if (mysql_num_rows($result) == 0) {
        echo "No rows found.";
    } else {
        while ($row = mysql_fetch_assoc($result)) {
            while ($cols = mysql_fetch_assoc($rcols);) {
                $vegetable_list[$i][$j] = $row[$cols['Field']];
            $i++;
            }
            $j++;
        }
    }
} else {
    //some error message
}

答案 3 :(得分:1)

使用开关案例:

$i=1;

while($row_result=mysql_fetch_assoc($result))
{
     switch($i) {
         case 1 : $rice = $row_result[$i]; break;
         case 2 : $wheat = $row_result[$i]; break;
     }
     $i++;
}

或者你可以这样做:

$i=0;
$vegetable = array("rice", "wheat", "potato");
while($row_result=mysql_fetch_assoc($result))
{
     $idx = 0;
     foreach($row_result as $result){
          ${$vegetable[$i]}[$idx] = $result;
          $idx++;
     }
     $i++;
}

然后尝试

var_dump($rice);

您应该获得特定列的数组。

答案 4 :(得分:0)

首先,你不需要将数组元素作为变量。 你可以简单地写下没有美元符号,如:米饭作为米饭等等。

$vegetable_list= array('rice', 'wheat', 'potato', 'pulses');

现在执行Select query以从数据库中获取数据

$query = "select * from test where value=20";
$result = mysql_query($query); 

make while循环

 while($row_result=mysql_fetch_row($result))
{
foreach($vegetable_list as $k => $v)
     ${$v} = $row_result[$k + 1]; 
   from 0 -> n (not from 1)
}
相关问题