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

?>

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 .

2 Answers 2

3

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;
}
$json = json_encode($arr);

?>
Sign up to request clarification or add additional context in comments.

Comments

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;
}
echo json_encode($arr);

?>

try this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.