15 mysqli
1 mysqli
1.1 mysqli连接数据库
<?php
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'sora';
$id = $_POSt['id'];
$pwd = $_POST['pwd'];
$connect = new mysqli($hostname,$username,$password,$database);
if(!$connect){
die("connection failed:".mysqli_connect_error());
}
//设置字符集位utf8
$connect->set_charset('utf8');
$sql = "select id,passsword from temp1 where id = $id and password = $pwd";
$res = $connect->query($sql);
用到的函数:
1.1.1 mysqli::$connect_error || mysqli_connect_error
(PHP 5, PHP 7, PHP 8)
mysqli::$connect_error -- mysqli_connect_error — Returns a description of the last connection error
说明
面向对象风格
?string $mysqli->connect_error;
过程化风格
mysqli_connect_error(): ?string
Returns the error message from the last connection attempt.
参数
此函数没有参数。
返回值
A string that describes the error. null
is returned if no error occurred.
1.1.2 mysqli::query mysqli_query
(PHP 5, PHP 7, PHP 8)
mysqli::query -- mysqli_query — 对数据库执行一次查询
说明
面向对象风格
mysqli::query(string $query
, int $resultmode
= MYSQLI_STORE_RESULT): mixed
过程化风格
mysqli_query(mysqli $link
, string $query
, int $resultmode
= MYSQLI_STORE_RESULT): mixed
Performs a query
against the database.
For non-DML queries (not INSERT, UPDATE or DELETE), this function is similar to calling mysqli_real_query() followed by either mysqli_use_result() or mysqli_store_result().
注意:
In the case where you pass a statement to mysqli_query() that is longer than max_allowed_packet
of the server, the returned error codes are different depending on whether you are using MySQL Native Driver (mysqlnd
) or MySQL Client Library (libmysqlclient
). The behavior is as follows:
mysqlnd
on Linux returns an error code of 1153. The error message means got a packet bigger thanmax_allowed_packet
bytes.mysqlnd
on Windows returns an error code 2006. This error message means server has gone away.libmysqlclient
on all platforms returns an error code 2006. This error message means server has gone away.
参数
link
仅以过程化样式:由mysqli_connect() 或 mysqli_init() 返回的链接标识。
query
The query string.
Data inside the query should be properly escaped.
resultmode
Either the constant MYSQLI_USE_RESULT
or MYSQLI_STORE_RESULT
depending on the desired behavior. By default, MYSQLI_STORE_RESULT
is used.
If you use MYSQLI_USE_RESULT
all subsequent calls will return error Commands out of sync
unless you call mysqli_free_result()
With MYSQLI_ASYNC
(available with mysqlnd), it is possible to perform query asynchronously. mysqli_poll() is then used to get results from such queries.
返回值
失败时返回 false
,通过mysqli_query() 成功执行SELECT, SHOW, DESCRIBE
或 EXPLAIN
查询会返回一个mysqli_result 对象,其他查询则返回true
。
1.2 mysqli预处理
预处理有技术有两点好处:
1) 效率高,判断了这个语句是否已经编译过
2) 防止了sql注入
1.2.1 一个有点蠢的sql注入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>sql injection</title>
</head>
<body>
<form action="" method="POST">
id号:<input type="text" name="id">
<br/>
密码:<input type="password" name="pwd">
<input type="submit" value="提交">
</form>
</body>
</html>
<?php
//差不多就是是跟这sqli-labs写了一遍代码
//中途还是出现了一些问题
//只有写过代码之后才知道不足之处
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'sora';
$connect = new mysqli($hostname,$username,$password,$database);
if(!$connect){
die("connection failed:".mysqli_connect_error());
}
//设置字符集位utf8
$connect->set_charset('utf8');
if(isset($_POST['id']) && isset($_POST['pwd'])){
$id = $_POST['id'];
$pwd = $_POST['pwd'];
@$sql = "SELECT id,password from temp1 where id = $id and password = '$pwd'";
$res = $connect->query($sql);
echo $connect->error;
if($row = $res->fetch_assoc()){
echo 'id :' . $row['id'] . '密码 :' . $row['password'];
}else{
echo 'testing text';
}
}
?>
1.2.2 预处理改进
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>sql injection</title>
</head>
<body>
<form action="" method="POST">
id号:<input type="text" name="id">
<br/>
密码:<input type="password" name="pwd">
<input type="submit" value="提交">
</form>
</body>
</html>
<?php
//差不多就是是跟这sqli-labs写了一遍代码
//中途还是出现了一些问题
//只有写过代码之后才知道不足之处
$hostname = 'localhost';
$username = 'root';
$password = 'root';
$database = 'sora';
$connect = new mysqli($hostname,$username,$password,$database);
if(!$connect){
die("connection failed:".mysqli_connect_error());
}
//设置字符集位utf8
$connect->set_charset('utf8');
if(isset($_POST['id']) && isset($_POST['pwd'])){
$id = $_POST['id'];
$pwd = $_POST['pwd'];
@$sql = "SELECT id,password from temp1 where id = ? and password = ?";
/* 基本上就是跟着手册写的 */
if($stmt = $connect->prepare($sql)){
/* 绑定参数 */
$stmt->bind_param('is',$id,$pwd);
/* 执行查询 */
$stmt->execute();
/* 将查询结果绑定到变量中 */
$stmt->bind_result($res_id,$res_pwd);
/* 取出结果 */
$stmt->fetch();
echo 'id :' . $res_id . '密码 :' . $res_pwd;
/* 关于语句对象 */
$stmt->close();
}
}
//关闭连接
$connect->close();
?>
虽然还是蠢,但确实防止了注入,大概,当然代码还是有问题,只是我知识还不到位。
本文作者:xiaoovo
本文链接:https://www.cnblogs.com/xiaoovo/p/15968162.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步