2013-01-10 63 views
0
<?php 
include '../connection.php'; 

$sql="SELECT userid,name,batch FROM dbusers"; 
$results=mysql_query($sql) or die("Cannot execute query"); 
$count=mysql_num_rows($results); 
$arr=array(); 
for($i=0; $i < $count; $i++){ 
$rows=mysql_fetch_array($results); 
//What to put here ? 
} 
json_encode($arr); 

?> 

這是我的php代碼。我想問一下在for循環中放什麼,這樣我就可以在php中創建一個數組數組。內部數組將具有用戶標識,名稱和批處理作爲其元素。在for循環中創建Json數組 - php

回答

3

該放什麼?

$arr[] = $rows;

的完整代碼

<?php 
include '../connection.php'; 

$sql="SELECT userid,name,batch FROM dbusers"; 
$results=mysql_query($sql) or die("Cannot execute query"); 
$count=mysql_num_rows($results); 
$arr=array(); 
for($i=0; $i < $count; $i++){ 
    $rows=mysql_fetch_array($results, MYSQL_ASSOC);//use MYSQL_ASSOC so you wouldn't have duplicate data 
    $arr[] = $rows; 
} 
var json = json_encode($arr); 

?> 
+0

Thanx它的工作..你們在StackOverflow是真的有用像我這樣的兒童開發.. .. – omerjerk

+0

你真棒@穆薩 – mahmud

0
<?php 
include '../connection.php'; 

$sql  = "SELECT userid,name,batch FROM dbusers"; 
$results = mysql_query($sql) or die("Cannot execute query"); 
$arr  = array(); 

while($rows = mysql_fetch_assoc($results)){ 
$arr[]  = $row; 
} 
json_encode($arr); 

?> 

嘗試。