Thanks everyone, question answered! If anyone is interested, the updated function is as follows, all other code remains the same:
function fetch_questions($page) {
global $link;
$proc = mysqli_prepare($link, "SELECT * FROM tquestions_cwh 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);
// $rows[]=$rowq;
// }
while ($proc->fetch())
{
foreach($rowq as $key=>$value )
{
$row_tmb[ $key ] = $value;
}
$rows[] = $row_tmb;
}
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
return($rows);
}
Ok,
Here is the code:
function fetch_questions($page) {
global $link;
$proc = mysqli_prepare($link, "SELECT * FROM tquestions_cwh WHERE page = ?");
mysqli_stmt_bind_param($proc, "i", $page);
mysqli_stmt_execute($proc);
$rows = array();
stmt_bind_assoc($proc, $rowq);
// loop through all result rows
while ($proc->fetch()) {
// print_r($rowq);
$rows[]=$rowq;
}
mysqli_stmt_close($proc);
mysqli_clean_connection($link);
return($rows);
}
I then add this to a php variable, like so:
$qs = fetch_questions($page);
I then loop through is, like so:
foreach($qs as $value){
echo "<tr>".$value['qnum']." is the questions number and the question text is ".$value['qtext'].". The page and q values are ".$value['page']." and ".$value['questions']." respectively.</tr>";
The output, however is this:
8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how开发者_C百科 what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team's objectives. The page and q values are 1 and q8 respectively.
Which is not what I want, for information purposes, the array using the print function looks like this:
Array
(
[0] => Array
(
[questions] => q8
[qnum] => 8
[qtext] => I know how what I do fits into my team's objectives
[page] => 1
)
[1] => Array
(
[questions] => q8
[qnum] => 8
[qtext] => I know how what I do fits into my team's objectives
[page] => 1
)
[2] => Array
(
[questions] => q8
[qnum] => 8
[qtext] => I know how what I do fits into my team's objectives
[page] => 1
)
[3] => Array
(
[questions] => q8
[qnum] => 8
[qtext] => I know how what I do fits into my team's objectives
[page] => 1
)
[4] => Array
(
[questions] => q8
[qnum] => 8
[qtext] => I know how what I do fits into my team's objectives
[page] => 1
)
[5] => Array
(
[questions] => q8
[qnum] => 8
[qtext] => I know how what I do fits into my team's objectives
[page] => 1
)
[6] => Array
(
[questions] => q8
[qnum] => 8
[qtext] => I know how what I do fits into my team's objectives
[page] => 1
)
[7] => Array
(
[questions] => q8
[qnum] => 8
[qtext] => I know how what I do fits into my team's objectives
[page] => 1
)
)
Clearly it's not looping through and displaying each row as it should...any advice?
Homer.
I read the manual and find this may help you:
/*
while ($proc->fetch()) {
// print_r($rowq);
$rows[]=$rowq;
}
*/
while ($proc->fetch())
{
foreach($rowq as $key=>$value )
{
$row_tmb[ $key ] = $value;
}
$row[] = $row_tmb;
}
quote: The problem is that the $rowq
returned is reference and not data. So, when you write $row[] = $rowq
, the $row
will be filled up with the last element of the dataset.
Sorry I'm not that familiar with the procedural style of mysqli so I'm not sure if this is your problem or not. However, it seems to me that you've set $proc to be the result of mysqli_prepare but according to the manual, this function returns true or false. You've then used $proc as the first parameter for mysqli_stmt_bind_param() but, again according to the manual, this parameter should be the return of mysqli_stmt_init() which I assume is $link. In fact, wherever you have used $proc as a parameter, you should instead use $link.
mysqli_stmt_fetch manual says:
Note: Note that all columns must be bound by the application before calling mysqli_stmt_fetch().
Maybe for a SELECT *
query you should use mysqli_stmt_store_result
and mysqli_stmt_result_metadata
functions to get all columns.
Not sure if there is more, but the markup is wrong: you're missing opening and closing <td>
. The output you posted seems complete, it just appears all in one line. Try:
foreach($qs as $value) {
echo "<tr><td>".$value['qnum']." is the questions number and the question text is ".$value['qtext'].". The page and q values are ".$value['page']." and ".$value['questions']." respectively.</td></tr>";
}
精彩评论