我一直在試圖找到MySQLi的解決方案,它沒有獲取數組函數,並且我找到了這個有趣的代碼位。你認爲這個代碼值得使用,沒有巨大的安全漏洞?MySQLi準備的結果數組
/*
* Utility function to automatically bind columns from selects in prepared statements to
* an array
*/
function bind_result_array($stmt)
{
$meta = $stmt->result_metadata();
$result = array();
while ($field = $meta->fetch_field())
{
$result[$field->name] = NULL;
$params[] = &$result[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
return $result;
}
/**
* Returns a copy of an array of references
*/
function getCopy($row)
{
return array_map(create_function('$a', 'return $a;'), $row);
}
信用:http://gunjanpatidar.wordpress.com/2010/10/03/bind_result-to-array-with-mysqli-prepared-statements/
按常理要求:
$db = new PDO("mysql:host='localhost';dbname='testing'", 'username', 'password') or die('Could not connect to server');
$get_posts = mysqli_stmt_init($db);
mysqli_stmt_prepare($get_posts, 'select * from Chatposts where Chatid = ? and CPid > ? and Deleted = ? order by CPid desc limit ?');
mysqli_stmt_bind_param($get_posts, 'iiii', $chatroomid, $lastpost, $deleted, $limit);
mysqli_stmt_execute($get_posts);
mysqli_stmt_bind_result($get_posts, $newcolumn['ID'], $newcolumn['Chatid'], $newcolumn['Name'], $newcolumn['URL'], $newcolumn['Text'], $newcolumn['Datetime'], $newcolumn['IPaddress'], $newcolumn['Deleted']);
mysqli_stmt_store_result($get_posts);
mysqli_stmt_fetch($get_posts); // Trying to fetch array
mysqli_stmt_close($get_posts);
我寫我的代碼程序,因爲我覺得這樣更容易讀寫,尤其是學習你的代碼。在連接的OOP和其他代碼的程序之間跳轉會有什麼問題嗎?學習一點OOP比處理STMT更容易。 – Rujikin 2013-03-14 07:41:41
如果您通過添加查詢處理的完整代碼來編輯您的問題,從定義查詢到獲取所有數據,我將能夠將其重寫爲PDO以向您顯示OOP樣式實際上是短10倍且更清晰可讀的寫。 – 2013-03-14 09:24:25
如果我看到我的代碼的直接副本,學習會更容易。清理了代碼並更新了OP。提前致謝!! – Rujikin 2013-03-14 17:01:08