开发者

PHP: insert query result into array elements

开发者 https://www.devze.com 2023-01-18 14:18 出处:网络
How can I assign query result into array elements? This is my code: include(\'db.php\'); $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(\"Database connection error\");

How can I assign query result into array elements?

This is my code:

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

How can I assign the query result into the array? Let's say:

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

How I can assign the values automatically?


Let try:

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


Ok not sure but seems like you have a 150column x 20rows table that you want to convert into a two dimensional array. It is as simple as this:

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


Edited the code.

Here:

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
}


use switch case :

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

or you can do this :

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

then try to

var_dump($rice);

you should get array of your specific column.


The first thing is that, you dont need to make array element as variable. You can simply write without dollar sign like : $rice as rice and so on.

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

Now execute Select query to get the data from database

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

make while loop

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

精彩评论

暂无评论...
验证码 换一张
取 消