Reputation: 3323
Thanks for the contributions so far - I can see now that $rowq is not a single array but lots of them - I'd like to bring all the rows back in the array, any suggestions?
First off, the code which brings back my data into an array:
function fetch_questions($page) {
global $link;
$proc = mysqli_prepare($link, "SELECT * FROM tques WHERE page = ?");
mysqli_stmt_bind_param($proc, "i", $page);
mysqli_stmt_execute($proc);
$rowq = array();
stmt_bind_assoc($proc, $rowq);
// loop through all result rows
while ($proc->fetch()) {
// print_r($rowq);
}
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
return($rowq);
}
Now, when I `print_r($rowq);' I get the following, which is all good:
Array ( [questions] => q1 [qnum] => 1 [qtext] => I find my job meaningful [page] => 1 ) Array ( [questions] => q2 [qnum] => 2 [qtext] => I find my job interesting [page] => 1 ) Array ( [questions] => q3 [qnum] => 3 [qtext] => My work supports ABC's objective [page] => 1 ) Array ( [questions] => q4 [qnum] => 4 [qtext] => I am able to balance my work and home life [page] => 1 ) Array ( [questions] => q5 [qnum] => 5 [qtext] => I am clear about what is expected of me in my job [page] => 1 ) Array ( [questions] => q6 [qnum] => 6 [qtext] => My induction helped me to settle into my job [page] => 1 ) Array ( [questions] => q7 [qnum] => 7 [qtext] => I understand the ABC vision [page] => 1 ) Array ( [questions] => q8 [qnum] => 8 [qtext] => I know how what I do fits into my team's objectives [page] => 1 )
Now, in my php page I have the following piece of script:
$questions = fetch_questions($page);
And when I print_r $questions, as below:
print_r($questions);
I only get the following back from the array, 1 row:
Array ( [questions] => q8 [qnum] => 8 [qtext] => I know how what I do fits into my team's objectives [page] => 1 )
Any ideas why that might be?
Thanks in advance,
Homer.
Upvotes: 1
Views: 102
Reputation: 22783
You're not collecting the results in an array. You were print_r
-ing each individual row in the while
loop.
So if you look carefully at the results you show, this is what actually happened:
// in each iteration of the while loop
// print_r( $rowq )
Array ( [questions] => q1 [qnum] => 1 [qtext] => I find my job meaningful [page] => 1 )
// print_r( $rowq )
Array ( [questions] => q2 [qnum] => 2 [qtext] => I find my job interesting [page] => 1 )
// print_r( $rowq )
Array ( [questions] => q3 [qnum] => 3 [qtext] => My work supports ABC's objective [page] => 1 )
// etc..
It just presented itself behind each other.
So effectively you are only returning the last row from the while loop when calling the function.
As a side note:
You are not using mysqli_prepare
as intended. You should replace $page
with a ? placeholder, like so:
$proc = mysqli_prepare($link, "SELECT * FROM tques WHERE page = ?");
// the following statement will properly replace the placeholder with $page
mysqli_stmt_bind_param($proc, "i", $page);
Upvotes: 3
Reputation: 94143
Your $rowq
variable is only holding the last row that was fetched. When you print_r
during your fetch loop, each row is fetched into the $rowq
variable, and then immediately printed, but because you overwrite the variable each iteration, when the loop completes only the final row is contained within $rowq
.
If you want to hang onto all the rows, you can augment your function:
function fetch_questions($page) {
// ...
$rows = array();
while ($proc->fetch()) {
$rows[] = $rowq;
}
// ...
return $rows;
}
With this code (only the relevant parts are included), every time a row is fetched, it is placed within an array $rows
, which then contains all rows when the loop completes.
Upvotes: 3