第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

老師,如果select語(yǔ)句寫(xiě)得是select * 那么 bind_result里面該怎么寫(xiě)呢?

老師,如果select語(yǔ)句寫(xiě)得是select * 那么 bind_result里面該怎么寫(xiě)呢?不會(huì)把數(shù)據(jù)的所有字段都寫(xiě)上吧?

正在回答

2 回答

A note to people to want to return an array of results - that is, an array of all the results from the query, not just one at a time.

<?php

// blah blah...
call_user_func_array(array($mysqli_stmt_object,?"bind_result"),?$byref_array_for_fields);

$results?= array();
while ($mysqli_stmt_object->fetch()) {
? ??$results[] =?$byref_array_for_fields;
}

?>
This will NOT work. $results will have a bunch of arrays, but each one will have a reference to $byref.

PHP is optimizing performance here: you aren't so much copying the $byref array into $results as you are *adding* it. That means $results will have a bunch of $byrefs - the same array repeated multiple times. (So what you see is that $results is all duplicates of the last item from the query.)

hamidhossain (01-Sep-2008) shows how to get around that: inside the loop that fetches results you also have to loop through the list of fields, copying them as you go. In effect, copying everything individually.

Personally, I'd rather use some kind of function that effectively duplicates an array than write my own code. Many of the built-in array functions don't work, apparently using references rather than copies, but a combination of array_map and create_function does.

<?php

// blah blah...
call_user_func_array(array($mysqli_stmt_object,?"bind_result"),?$byref_array_for_fields);

// returns a copy of a value
$copy?=?create_function('$a',?'return $a;');

$results?= array();
while ($mysqli_stmt_object->fetch()) {
? ??// array_map will preserve keys when done here and this way
? ??$results[] =?array_map($copy,?$byref_array_for_fields);
}

?>

All these problems would go away if they just implemented a fetch_assoc or even fetch_array for prepared statements...

0 回復(fù) 有任何疑惑可以回復(fù)我~

在百度知道中看到一個(gè)方法,原問(wèn)題點(diǎn)這里,還挺巧妙的:

$result?=?$mysqli_stmt->result_metadata();
$fields?=?$result->fetch_fields();
foreach($fields?as?$field){
????$column[]=&$out[$field->name];
}
call_user_func_array(array($mysqli_stmt,'bind_result'),$column);
while?($mysqli_stmt->fetch())?{
????$t=array();
????foreach($out?as?$key=>$val){
????????$t[$key]=$val;
????}
????$res[]=$t;
}


0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消

老師,如果select語(yǔ)句寫(xiě)得是select * 那么 bind_result里面該怎么寫(xiě)呢?

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)