Initialize an array with database query results

Source: Internet
Author: User
Use the database query results to initialize the array database php array

The database query code is as follows:
$query = mssql_query($sql_all);$array_pie=new array();while($row=mssql_fetch_array($query)){//array_push($arry_pie, $var)//???}

To initialize the query result $ array_pie, the structure is similar to the following array.
var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]); 

How to write the code in while?


Reply to discussion (solution)

The result you want is actually a two-dimensional array.

$query = mssql_query($sql_all);$array_pie=new array();$i = 0;while($row=mssql_fetch_array($query)){array_pie[$i][] = $row;$i++;}

var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]);
Corresponding data in
$ Row [1], $ row [2]

The result you want is actually a two-dimensional array.

$query = mssql_query($sql_all);$array_pie=new array();$i = 0;while($row=mssql_fetch_array($query)){array_pie[$i][] = $row;$i++;}



1
Var myData = new Array ([1997, 7.80], [1998, 4.80], [1999, 6.50]);
Corresponding data in
$ Row [1], $ row [2]

var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]);
Corresponding data in
$ Row [1], $ row [2]

$query = mssql_query($sql_all);$array_pie=new array();$i = 0;while($row=mssql_fetch_array($query)){array_pie[$i][0] = $row[1];        array_pie[$i][1] = $row[2];$i++;}

Try this way

$query = mssql_query($sql_all);$array_pie = array();while($row=mssql_fetch_array($query)){  $array_pie[] = array($row[1]、$row[2]);}


var myData = 
 ;


var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]);
Corresponding data in
$ Row [1], $ row [2]

$query = mssql_query($sql_all);$array_pie=new array();$i = 0;while($row=mssql_fetch_array($query)){array_pie[$i][0] = $row[1];        array_pie[$i][1] = $row[2];$i++;}

Try this way
How to output $ array_pie to check whether the data is correct?

Print_r ($ array_pie) outside the while loop );

$query = mssql_query($sql_all);$array_pie = array();while($row=mssql_fetch_array($query)){  $array_pie[] = array($row[1]、$row[2]);}


var myData = 
 ;

$query1 = mssql_query($sql_all);$arry_pie = array();while($row1=mssql_fetch_array($query1)){//echo $row;//array arrayif($row1[2]!=0){$row1['percent_d']=round($row1[2]/$s_ds*100,2).'%';}else{$row1['percent_d']=0;}if($row1[4]!=0){$row1['percent_m']=round($row1[4]/$s_ms*100,2).'%';}else{$row1['percent_m']=0;}//$array_pie[$i][0]=$row[0];//$arry_pie[$i][1]=$row[2];$arry_pie=array($row1[0],$row1[1]);//$i++;array_push($items, $row1);}echo json($arry_pie);

Baoc
Fatal error: Call to undefined function json () in C: \ PHP \ APMServ5.2.6 \ www \ htdocs \ xjjxV2 \ get_brands.php on line 47

Hand error
Json should be json_encode

Hand error
Json should be json_encode
Why is there only one data output...

$arry_pie = array();$query = mssql_query($sql_all);while($row=mssql_fetch_array($query)){$s_dq=$s_dq+$row[1];$s_ds=$s_ds+$row[2];$s_mq=$s_mq+$row[3];$s_ms=$s_ms+$row[4];$arry_pie=array($row[0],$row[1]);}echo json_encode($arry_pie)."
";

$ Arry_pie = array ($ row [0], $ row [1]);
Change
$ Arry_pie [] = array ($ row [0], $ row [1]);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.