1

I'm fairly new to php and mysql. I'm trying to create a rest api from php, and because my server doesn't have mysqlndinstalled, I have to use bind_result and fetch.

$stmt = $this->conn->prepare("SELECT * from d WHERE d.id = ?");
$stmt->bind_param("i", 1);
if($stmt->execute()){
    $stmt->bind_result($a, $b, $c);
    $detail = array();  
    while($stmt->fetch()){      

        $detail["a"] = $a;
        $detail["b"] = $b;
        $detail["c"] = $c;
    }

    $stmt->close();
    return $response;
} else {
    return NULL;
}

Above code works but it can only return 1 line of information at a time.

For example if the statement return:

a    b    c
1   test  test
1   test1 test1

it only returns

a: 1
b: test1
c: test1

where its supposed to be:

{
    a: 1
    b: test
    c: test
},
{
    a: 1
    b: test1
    c: test1
}
2
  • You're replacing $detail on every iteration of the while loop. You need to append via $detail[]["a"] = $a and so on. Commented Oct 19, 2014 at 4:45
  • Are you on shared webhosting or your own box? Is there a way to install mysqlnd? Commented Oct 19, 2014 at 4:47

1 Answer 1

1

You're overwritting them, you could do something like this instead:

$detail = array();  
while($stmt->fetch())
{           
    $temp = array():
    $temp["a"] = $a;
    $temp["b"] = $b;
    $temp["c"] = $c;
    $detail[] = $temp;
}

Or directly appending them with another dimension:

$detail = array();  
while($stmt->fetch()) {           
    $detail[] = array('a' => $a, 'b' => $b, 'c' => $c);
      //   ^ add another dimension
}
Sign up to request clarification or add additional context in comments.

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.