1

I have the following php code:

$row=$this->conn->prepare('SELECT * FROM users');  

$row->execute();//execute the query  
$json_data=array();//create the array  
foreach($row as $r)
{  
    $json_array = array();
    $json_array['id'] = $r['id'];  
        $json_array['unique_id'] = $r['unique_id'];  
        $json_array['name'] = $r['name'];  
        $json_array['email'] = $r['email'];  
        $json_array['encrypted_password'] = $r['encrypted_password'];  
        $json_array['salt'] = $r['salt'];  
        $json_array['created_at'] = $r['created_at'];  
        $json_array['updated_at'] = $r['updated_at'];  
        $json_array['phone_number'] = $r['phone_number']; 

    //here pushing the values in to an array  
        array_push($json_data,$json_array);  

}  

//built in PHP function to encode the data in to JSON format  
return json_encode($json_data);

When i run it i am only getting this:

[{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null},{"id":null,"unique_id":null,"name":null,"email":null,"encrypted_password":null,"salt":null,"created_at":null,"updated_at":null,"phone_number":null}]

My database contains only 2 users and they don't have null columns.

4
  • 2
    What is $this->conn ? I believe you need to iterate on something like $row->fetch(), not just $row Commented Jan 5, 2016 at 19:12
  • are you able to fetch data? Commented Jan 5, 2016 at 19:13
  • 1
    You're not fetching the data... fetch_all(PDO::FETCH_ASSOC) Commented Jan 5, 2016 at 19:14
  • $result = $row->fetchAll(); after execute try Commented Jan 5, 2016 at 19:58

1 Answer 1

1

$row is an instance of mysqli_stmt; you cannot use it directly as a traversable in foreach() like a mysqli_result instance ( PHP >= 5.4.0).

$statement = $this->conn->prepare('
    SELECT
        id,
        unique_id,
        name,
        email,
        encrypted_password,
        salt,
        created_at,
        updated_at,
        phone_number
    FROM
        users
');

if ( !$statement ) {
    yourErrorHandler();
}

if ( !$statemnt->execute() ) {
    yourErrorHandler();
}

$result = $statement->get_result(); // PHP 5 >= 5.3.0,
$json_data=[];
foreach($result as $row) { // PHP 5 >= 5.4.0
    $json_data[] = $row;
}
return json_encode($json_data);

alternative version:

$result = $this->conn->query('
    SELECT
        id,
        unique_id,
        name,
        email,
        encrypted_password,
        salt,
        created_at,
        updated_at,
        phone_number
    FROM
        users
');

if ( !$result ) {
    yourErrorHandler();
}

return json_encode( $result->fetch_all(MYSQLI_ASSOC) );
Sign up to request clarification or add additional context in comments.

3 Comments

I've tried the first one and it keeps that i'm callind to undefined method get_results. I've tried fetch and still not working. The alternative version gives me this error: "Call to undefined method mysqli_result::fetch_all()"
Nevermind, i've fixed it. I had to manually fetch the result. THANKS ALOT! I've been struggling on this for 2 days. love you!
fetch_all() has been introduced with php 5.3.0, are you still using a version <5.3 ?

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.