执行原生SQL语句
执行查询SQL语句
执行查询语句
/**
* 执行查询语句
*
* @param $sql 执行的SQL语句
* @param null $params 绑定的参数
* @param null $row_type 返回单行还是多行,默认多行
* @param int $fetch_mode 获取模式
* @return array|\PDOStatement
*/
query($sql, $params, $row_type, $fetch_mode)
$users = $db->query('SELECT * FROM user WHERE type = 2');
获取单条(对query方法的封装)
$user = $db->one('SELECT * FROM user WHERE id = 1 LIMIT 1');
var_dump($user);
//使用参数绑定
$user = $db->one('SELECT * FROM user WHERE id = ? LIMIT 1', [1]);
获取多条(对query方法的封装)
$users = $db->all('SELECT * FROM user where cid > 2 status = 1 LIMIT 5');
var_dump($rows);
//使用参数绑定
$users = $db->all('SELECT * FROM user where cid > ? status = ? LIMIT 5', [2, 1]);
执行非查询SQL语句
$sql = "UPDATE timo_user SET nickname = ? WHERE id = ?";
$affected_rows = $db->execute($sql, ['tommy8', 1], true);
if ($affected_rows !== false) {
echo '更新成功';
} else {
echo '更新失败';
}
获取最新的insert_id
$insert_id = $db->lastInsertId();