3
我試圖從數據庫中獲取一些數據,然後將其傳遞到數組中以供將來使用。我正在使用MySQLi作爲我的驅動程序。數組從數據庫獲取數據時遇到的問題
這裏是我的代碼:
// Build a query to get skins from the database
$stmt = $mysqli->prepare('SELECT id, name, description, author, timestamp, url, preview_filename FROM `skins` LIMIT 0, 5');
$stmt->execute();
$stmt->bind_result($result['id'], $result['name'], $result['desc'], $result['auth'], $result['time'], $result['url'], $result['preview']);
// The skins array holds all the skins on the current page, to be passed to index.html
$skins = array();
$i = 0;
while($stmt->fetch())
{
$skins[$i] = $result;
$i++;
}
print_r($skins);
的問題是,這種執行時,該$skins
數組包含從查詢的最後結果行。這是$獸皮的print_r:
Array
(
[0] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
[1] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
[2] => Array
(
[id] => 3
[name] => sdfbjh
[desc] => isdbf
[auth] => dfdf
[time] => 1299970810
[url] => http://imgur.com/XyYxs.png
[preview] => 011e5.png
)
)
正如你可以看到,從查詢的最後結果是所有的填充某種原因數組條目。
任何人都可以解釋這種行爲,並告訴我我做錯了什麼?謝謝。 :)
編輯:這裏的解決方案:
while($stmt->fetch())
{
foreach($result as $key=>$value)
{
$tmp[$key] = $value;
}
$skins[$i] = $tmp;
$i++;
}
完美,謝謝。我會用解決方案編輯我的文章,並儘可能地接受您的答案。 – Josh 2011-03-14 20:56:03
不客氣:-)玩得開心! – 2011-03-14 20:56:52