0

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.

6
  • What are you expecting to see and what you see actually? Commented Feb 25, 2011 at 6:17
  • isnt $json[]=array( 'courses'=>$row['course1'], ); causing the $json array to be overwritten ? Commented Feb 25, 2011 at 6:20
  • 1
    @Clyde : no, it will push new array to the existing $json[] Commented Feb 25, 2011 at 6:23
  • @Gaurav : thanx , its been ages since have coded something useful in php ;) Commented Feb 25, 2011 at 6:24
  • Why don't you try echo mysql_num_rows($result); to check the number of rows returned ? Commented Feb 25, 2011 at 6:26

4 Answers 4

2

It seems that your database has wrong design.

These courses should be stored in a separate table, not in the users table.
And then it can be easily retrieved.

To answer more certainly, more data of your database structure is required.

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

Comments

0

What if you're doing something like that:

<?php
...
$json = array();
$json['courses'] = array();
if($result = $mysqli->query("select course1 from users where username ='test'")) {
    while ($row=$result->fetch_assoc()) {
        $json['courses'][] = $row['course1'];
    }
}
...
?>

Comments

0

If you want to list only courses, you don't need to all these lines.

while ($row=$result->fetch_assoc()) {
    $json[]=array(
        'courses'=>$row['course1'],

    );
}

this will be enough

while ($row=$result->fetch_assoc()) {
    $json[]= $row['course1'] ;
}

Comments

0

Try to perform this:

$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 * from users where username ='test'")) {
    while ($row=$result->fetch_assoc()) {
        $json[]=array(
            'courses'=> array(
                  'cource1' => $row['course1'],
                  'cource2' => $row['course2'],
                  'cource3' => $row['course3'],
            ),
        );
    }
}
$result->close();
header("Content-Type: text/json");
echo json_encode(array( 'courses'  =>   $json )); 
$mysqli->close();

Assuming that you have the users table with fields named 'course1','course2','course3' etc and 'username'. You can perform * or just list required fields (comma-separated) after SELECT keyword in the sql query. For example, you have one user with name test:

username course1 course2 course3
  test     Yes     Yes     No

You'll receive just one row from the table. You'll get

//var_dump($json);

array(1) {
  [0]=>
  array(1) {
    ["cources"]=>
    array(3) {
      ["cource1"]=>
      string(3) "Yes"
      ["cource2"]=>
      string(3) "Yes"
      ["cource3"]=>
      string(2) "No"
    }
  }
}

after performing the code above. Then you just need to json_encode() this array. Hope that it'll help.

2 Comments

This returned "object Object" for me. Not the courses as an array
I haven't mentioned that the chunk of code above has to be inserted in your code. I've edited my code above

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.