PHP5 mysqli的prepare准备语句使用说明
mysqli对prepare的支持对于大访问量的网站是很有好处的,它极大地降低了系统开销,而且保证了创建查询的稳定性和安全性.prepare准备语句分为绑定参数和绑定结果
mysqli对prepare的支持对于大访问量的网站是很有好处的,它极大地降低了系统开销,而且保证了创建查询的稳定性和安全性。prepare准备语句分为绑定参数和绑定结果,下面将会一一介绍!
(1)绑定参数
看下面php代码:
代码如下:
//创建连接
$mysqli=new mysqli("localhost","root","","volunteer");
//检查连接是否被创建
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/*
* 创建一个准备查询语句:
* ?是个通配符,可以用在任何有文字的数据
* 相当于一个模板,也就是预备sql语句
*/
if ($stmt = $mysqli->prepare("insert into `vol_msg`(mid,content) values(?,?)")){
/*第一个参数是绑定类型,"s"是指一个字符串,也可以是"i",指的是int。也可以是"db",
* d代表双精度以及浮点类型,而b代表blob类型,第二个参数是变量
*/
$stmt->bind_param("is",$id,$content);
//给变量赋值
$id = "";
$content = "这是插入的内容";
//执行准备语句
$stmt->execute();
//显示插入的语句
echo "Row inserted".$stmt->affected_rows;
//下面还可以继续添加多条语句,不需要prepare预编译了
//关闭数据库的链接
$mysqli->close();
}
?>
以上php实例运行结果:
Row inserted:1
(2).绑定结果:绑定结果就是将你绑定的字段给php变量,以便必要时使用这些变量
请看下面的php代码:
代码如下:
//创建连接
$mysqli=new mysqli("localhost","root","","volunteer");
//设置mysqli编码
mysqli_query($mysqli,"SET NAMES utf8");
//检查连接是否被创建
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
//创建准备语句
if ($stmt = $mysqli->prepare("select mid,content from `vol_msg`")){
//执行查询
$stmt->execute();
//为准备语句绑定实际变量
$stmt->bind_result($id,$content);
//显示绑定结果的变量
while($stmt->fetch()){
echo "第".$id."条: ".$content."
";
}
//关闭数据库的链接
$mysqli->close();
}
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When writing web applications using PHP, a MySQL database is often used to store data. PHP provides a way to interact with the MySQL database called MySQLi. However, sometimes when using MySQLi, you will encounter an error message, as shown below: PHPFatalerror:Calltoundefinedfunctionmysqli_connect() This error message means that PHP cannot find my

PDOPDO is an object-oriented database access abstraction layer that provides a unified interface for PHP, allowing you to use the same code to interact with different databases (such as Mysql, postgresql, oracle). PDO hides the complexity of underlying database connections and simplifies database operations. Advantages and Disadvantages Advantages: Unified interface, supports multiple databases, simplifies database operations, reduces development difficulty, provides prepared statements, improves security, supports transaction processing Disadvantages: performance may be slightly lower than native extensions, relies on external libraries, may increase overhead, demo code uses PDO Connect to mysql database: $db=newPDO("mysql:host=localhost;dbnam

Using Prepared Statements Prepared statements in PDO allow the database to precompile queries and execute them multiple times without recompiling. This is essential to prevent SQL injection attacks, and it can also improve query performance by reducing compilation overhead on the database server. To use prepared statements, follow these steps: $stmt=$pdo->prepare("SELECT*FROMusersWHEREid=?");Bind ParametersBind parameters are a safe and efficient way to provide query parameters that can Prevent SQL injection attacks and improve performance. By binding parameters to placeholders, the database can optimize query execution plans and avoid performing string concatenation. To bind parameters, use the following syntax:

Solution to php unable to connect to mysqli: 1. Open the "php.ini" file; 2. Find "mysqli.reconnect"; 3. Change "mysqli.reconnect = OFF" to "mysqli.reconnect = on".

If you encounter the following error message when using PHP to connect to a MySQL database: PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused, then you can try to solve this problem by following the steps below. To confirm whether the MySQL service is running normally, you should first check whether the MySQL service is running normally. If the service is not running or fails to start, it may cause a connection refused error. you can

The running file of mysql is mysqld; mysqld is an executable file, which represents the Mysql server program. Executing this file can directly start a server process; and mysqld_safe is a startup script, which will indirectly call mysqld and also start a monitor. process.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

When using the mysqli extension to connect and operate a MySQL database, you sometimes encounter the error PHPFatalerror:Calltoundefinedmethodmysqli::prepare(). This error is usually caused by the following reasons: PHP has insufficient support for the mysqli extension; the mysqli extension is not loaded or configured correctly; there are syntax errors in the PHP code; the MySQL server is not correctly configured or running
