I'm trying to return a list of all of the courses a user is enrolled in (course1, course2, etc.) Currently, I have the following code:
$mysqli = new mysqli("localhost","username","password","sampleTest");
if (mysqli_connect_errno()) {
printf("Can't connect to SQL Server. Error Code %s\n", mysqli_connect_error($mysqli));
exit;
}
// Set the default namespace to utf8
$mysqli->query("SET NAMES 'utf8'");
$json = array();
if($result = $mysqli->query("select course1 from users where username ='test'")) {
while ($row=$result->fetch_assoc()) {
$json[]=array(
'courses'=>$row['course1'],
);
}
}
$result->close();
header("Content-Type: text/json");
echo json_encode(array( 'courses' => $json ));
$mysqli->close();
I can get the first course to show but not the others. I've tried select * from users where username ='test' but I'm stuck on the passing along the array.
$json[]=array( 'courses'=>$row['course1'], );causing the$jsonarray to be overwritten ?echo mysql_num_rows($result);to check the number of rows returned ?