开发者

PHP Populating an Array from a MySql Query

I am looking to create an array 开发者_如何学Pythonof data to be pass to another function that is populated from a database query and am not sure how to do this.

$dataArray[0][1];

$qry = mysql_query("SELECT Id, name FROM users");

while($res = mysql_fetch_array($qry)) {
    $dataArray[$res['Id']][$res['name']]
}

Thanks in advance.


This would look better

$dataArray = array();

$qry = mysql_query("SELECT Id, name FROM users");

while($res = mysql_fetch_array($qry)) {
    $dataArray[$res['Id']] = $res['name'];
}

you can take a look at the PHP manual how to declare and manipulate arrays.


The below code sniper is very handy...

$select=" WRITE YOUR SELECT QUERY "; $queryResult= mysql_query($select);

//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();

//STORE ALL THE RECORD SETS IN THAT ARRAY 
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC)) 
{
    array_push($data_array,$row);
}


mysql_free_result($queryResult);


//TEST TO SEE THE RESULT OF THE ARRAY 
echo '<pre>';
print_r($data_array);
echo '</pre>';

Thanks

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜