【发布时间】:2013-03-14 04:47:13
【问题描述】:
我一直在尝试为没有获取数组函数的 MySQLi stmt 找到解决方案,我发现了这段有趣的代码。你认为这段代码值得使用,没有巨大的安全漏洞吗?
/*
* 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);
【问题讨论】: