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

?>

This is my php code. I want to ask what to put inside the for loop so that I can create a an array of array in php. The inner array will have userid, name and batch as its elements .

share|improve this question

2 Answers

What to put here ?

$arr[] = $rows;

Full code

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

?>
share|improve this answer
Thanx it worked .. You guys on StackOverflow are really helpful for a child developer like me .. :) – omerjerk Jan 10 at 5:51
<?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);

?>

try this.

share|improve this answer

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.