Home > Backend Development > PHP Tutorial > 用数据库查询结果初始化数组

用数据库查询结果初始化数组

WBOY
Release: 2016-06-23 14:01:50
Original
1059 people have browsed it

数据库 php array

数据库查询代码如下:
$query = mssql_query($sql_all);$array_pie=new array();while($row=mssql_fetch_array($query)){	//array_push($arry_pie, $var)//???}
Copy after login

要让查询结果初始化$array_pie形成结构类似下面的数组
var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]); 
Copy after login

while中的代码该怎么写?


回复讨论(解决方案)

你想要的结果其实就是一个二维数组

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

var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]);
Copy after login
Copy after login
Copy after login
中数据分别对应
$row[1]、$row[2]

你想要的结果其实就是一个二维数组

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



1
var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]);
中数据分别对应
$row[1]、$row[2]

var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]);
Copy after login
Copy after login
Copy after login
中数据分别对应
$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++;}
Copy after login
Copy after login

这样试试

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


var myData = <?php echo json($array_pie);?>;
Copy after login
Copy after login


var myData = new Array([1997, 7.80], [1998, 4.80], [1999, 6.50]);
Copy after login
Copy after login
Copy after login
中数据分别对应
$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++;}
Copy after login
Copy after login

这样试试
怎么完整输出这个$array_pie以查看数据是否正确

在while循环外面 print_r($array_pie);

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


var myData = <?php echo json($array_pie);?>;
Copy after login
Copy after login

$query1 = mssql_query($sql_all);$arry_pie = array();while($row1=mssql_fetch_array($query1)){	//echo $row;//array array	if($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);
Copy after login

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

手误
json 应为 json_encode

手误
json 应为 json_encode
为什么输出只有一条数据...

$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)."<br>";
Copy after login

$arry_pie = array($row[0],$row[1]);
改为
$arry_pie[] = array($row[0],$row[1]);

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template