2010-10-05 36 views
-1

如何将查询结果分配给数组元素?PHP:将查询结果插入数组元素

这是我的代码:

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]; 

如何自动分配值?

+3

想如果这事做了_farm game_? – 2010-10-05 06:43:09

回答

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) 
} 
+0

可以给我一个例子..因为,而不是检索结果,我们必须将行结果的值分配到数组elements.pls..help .... – riad 2010-10-05 05:51:11

+0

它的工作原理。太棒了。这正是我寻找的。感谢很多Bang Dao ..感谢 - riad – riad 2010-10-07 10:04:32

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 
} 
+0

它不工作。我需要将查询结果值分配给数组元素。它显示每个循环的解析器error.on ..给我一个解决方案。 – riad 2010-10-05 05:49:14

+0

问题还没解决..是否可以请你帮我解决问题..我会非常高兴你.. – riad 2010-10-05 05:55:12

+0

编辑我的答案,请检查它的工作,否则请发布错误信息。 – Ruel 2010-10-05 05:57:23

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

你应该得到你的特定列的数组。

+0

非解决方案工作..thx – riad 2010-10-05 06:20:08

+0

对不起,它应该是mysql_fetch_assoc。我只是复制粘贴你的代码。现在应该是工作。 – 2010-10-05 06:27:13

+0

对不起兄弟。它也没有工作。但修改你的最后一个代码,它只能得到第一个element.pls的值,见下面的代码:$ i = 1; $ j = 0; $ vegetable = array(“rice”,“wheat”); while($ row_result = mysql_fetch_row($ result)) { $$ vegetable [$ j] = $ row_result [$ i]; $ i ++; $ j ++; } echo“Rice:”。$ rice; echo“
”; echo“Wheat:”。$ wheat; – riad 2010-10-05 06:35:12

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); 
0

第一件事就是,你不需要将数组元素作为变量。 你可以简单地写下如下的美元符号:$ rice as rice等等。

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

现在执行SELECT查询从数据库获取数据

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

使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) 
}