As you mentioned in comments, that you want to do ajax and json.
You can do code like this (use mysql_fetch_assoc() instead of array):
//set content type: text/json
header("Content-Type: text/json");
$dbhandle = mysql_connect("localhost","root","password") or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("info",$dbhandle) or die("Could not select examples");
$result = mysql_query("SELECT * FROM students");
if(mysql_num_rows($result)>0){
$rows=array();
while($row = mysql_fetch_assoc($result))
{
$rows[]=$row;
}
echo json_encode(array("success"=>$rows));
}else{
echo json_encode(array("error"=>"No records found."));
}
exit;
and for ajax (assuming, jQuery.get() AJAX)
$.get("script.php", function(data){
if(data.success){
//process each row
data.success.forEach(function(row){
console.log(row); //access student row here, simply like row.student_name, row.city etc.
});
}else{
alert(data.error);
}
});
Scurity Note:
- mysql_* functions are deprecated and will be removed from support, so you should avoid using it.
- use mysqli_* or PDO for database queries.