在 $bind_param(); 中动态绑定参数mysqli
Dynamically bind params in $bind_param(); Mysqli
我有数据库 class 正在处理所有对数据库的查询
我让 mysqli 准备工作正常。
bind_param 也工作正常,但问题是我想动态定义变量类型。
这是我的代码
public function query($sql, $params = array()){
$this->_error = false;
if($this->_query = $this->_mysqli->prepare($sql)){
$x = 1;
if(count($params)){
foreach($params as $param){
$this->_query->bind_param($x, $param);
$x++;
}
}
IN PDO fist 参数定义了我猜的位置所以这个函数通过每次设置 X = 1 和 x++ 运行良好,
但在 bind_param
中,我猜第一个参数定义了类型
正如 php.net 手册所说
那么如果用户按下我设置的整数值有什么办法
x = 我
对于字符串
x = 小号
依此类推所有 4 种类型......
喜欢
if((int)$param->){
x = i;
}
大家有什么想法吗?
提前致谢
对于类型来说很简单。一路使用s
即可。
还有一个更复杂的问题:事实上,你不能在循环中绑定,所以,必须使用call_user_func()
public function query($sql, $params = array())
{
if (!$params)
{
return $this->_mysqli->query($sql);
}
$stmt = $this->_mysqli->prepare($sql);
$types = str_repeat("s", count($params));
if (strnatcmp(phpversion(),'5.3') >= 0)
{
$bind = array();
foreach($values as $key => $val)
{
$bind[$key] = &$params[$key];
}
} else {
$bind = $values;
}
array_unshift($bind, $types);
call_user_func_array(array($stmt, 'bind_param'), $bind);
$stmt->execute();
return $stmt->get_result();
}
请注意,您不应将语句分配给局部变量,错误变量也没有用处。异常在各个方面都更好。
看上面的代码你要三思再转PDO,这么一个功能只需要三行代码:
public function query($sql, $params = array())
{
$stmt = $this->_pdo->prepare($sql);
$stmt->execute($params);
return $stmt;
}
如果您没有使用 PDO 的经验,这里是我写的 PDO tutorial,从中您将了解到它是最简单但功能强大的数据库 API,可以为您提供数十种不同格式的数据,代码量很少。
这是一个可能有用的示例(prepare()
函数是一个 class 方法)。
function prepare( $query, $bind = array() )
{
if ( !$stmt = $this->mysqli->prepare( $query ) )
throw new Exception( 'Query failed: ' . $query . PHP_EOL . $this->mysqli->error );
// if $bind is not an empty array shift the type element off the beginning and call stmt->bind_param() with variables to bind passed as reference
if ( $type = array_shift( $bind ) )
call_user_func_array(
array( $stmt, 'bind_param' ),
array_merge( array( $type ), array_map( function( &$item ) { return $item; }, $bind ) )
);
if ( !$stmt->execute() )
throw new Exception( 'Execute failed: ' . PHP_EOL . $stmt->error );
// choose what to return here ( 'affected_rows', 'insert_id', 'mysqli_result', 'stmt', 'array' )
}
用法示例:
$db->prepare( "SELECT * FROM user WHERE user_name = ? OR user_email = ?", [ 'ss', $user_name, $user_name ] );
我有数据库 class 正在处理所有对数据库的查询 我让 mysqli 准备工作正常。 bind_param 也工作正常,但问题是我想动态定义变量类型。 这是我的代码
public function query($sql, $params = array()){
$this->_error = false;
if($this->_query = $this->_mysqli->prepare($sql)){
$x = 1;
if(count($params)){
foreach($params as $param){
$this->_query->bind_param($x, $param);
$x++;
}
}
IN PDO fist 参数定义了我猜的位置所以这个函数通过每次设置 X = 1 和 x++ 运行良好,
但在 bind_param
中,我猜第一个参数定义了类型
正如 php.net 手册所说
那么如果用户按下我设置的整数值有什么办法
x = 我
对于字符串
x = 小号
依此类推所有 4 种类型......
喜欢
if((int)$param->){
x = i;
}
大家有什么想法吗?
提前致谢
对于类型来说很简单。一路使用s
即可。
还有一个更复杂的问题:事实上,你不能在循环中绑定,所以,必须使用call_user_func()
public function query($sql, $params = array())
{
if (!$params)
{
return $this->_mysqli->query($sql);
}
$stmt = $this->_mysqli->prepare($sql);
$types = str_repeat("s", count($params));
if (strnatcmp(phpversion(),'5.3') >= 0)
{
$bind = array();
foreach($values as $key => $val)
{
$bind[$key] = &$params[$key];
}
} else {
$bind = $values;
}
array_unshift($bind, $types);
call_user_func_array(array($stmt, 'bind_param'), $bind);
$stmt->execute();
return $stmt->get_result();
}
请注意,您不应将语句分配给局部变量,错误变量也没有用处。异常在各个方面都更好。
看上面的代码你要三思再转PDO,这么一个功能只需要三行代码:
public function query($sql, $params = array())
{
$stmt = $this->_pdo->prepare($sql);
$stmt->execute($params);
return $stmt;
}
如果您没有使用 PDO 的经验,这里是我写的 PDO tutorial,从中您将了解到它是最简单但功能强大的数据库 API,可以为您提供数十种不同格式的数据,代码量很少。
这是一个可能有用的示例(prepare()
函数是一个 class 方法)。
function prepare( $query, $bind = array() )
{
if ( !$stmt = $this->mysqli->prepare( $query ) )
throw new Exception( 'Query failed: ' . $query . PHP_EOL . $this->mysqli->error );
// if $bind is not an empty array shift the type element off the beginning and call stmt->bind_param() with variables to bind passed as reference
if ( $type = array_shift( $bind ) )
call_user_func_array(
array( $stmt, 'bind_param' ),
array_merge( array( $type ), array_map( function( &$item ) { return $item; }, $bind ) )
);
if ( !$stmt->execute() )
throw new Exception( 'Execute failed: ' . PHP_EOL . $stmt->error );
// choose what to return here ( 'affected_rows', 'insert_id', 'mysqli_result', 'stmt', 'array' )
}
用法示例:
$db->prepare( "SELECT * FROM user WHERE user_name = ? OR user_email = ?", [ 'ss', $user_name, $user_name ] );