Mysqli基础知识_MySQL
相信原来在开始学习php的时候,很多人使用的数据库首选MySQL,连接数据库的扩展首选mysql扩展,但随着php版本的提高,mysql扩展正逐渐被mysqli和PDO所取代。正如使用mysql函数时给出的deprecated: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead。学习mysqli扩展势在必行了。
相对于mysql扩展,mysqli扩展支持面向对象和面向过程两种方式,支持预处理,支持事务处理,而且速度比mysql速度更快。本篇将主要介绍mysqli的面向对象的基本简单操作。
mysqli安装配置
mysqli的安装配置和其他配置一样,首先要确认你的ext文件夹下存在php_mysqli.dll文件(一般而言是存在的),并且在php.ini文件中去掉“extension=php_mysqli.dll”这一行前的";",并且确定配置文件中extension_dir='ext目录所在的位置。重新启动服务器后,便可以使用mysqli扩展啦~
如何验证mysqli扩展已经开启了呢?
其实最直接的是使用mysqli扩展的函数看看能不能用,例如通过能否连接数据库判断扩展已经安装完成。连接成功,不必说,自然是装好了,连接不成功,也不要轻易认为没有装好,我们还有后招,利用phpinfo()函数,我们可以明确知道mysqli是否可用。
当然可以通过extension_loaded('mysqli') 来判断是否装载mysqli扩展,甚至可以通过get_loaded_extensions()获得到底装载了哪些扩展。
面向对象mysqli的使用
对于使用过mysql扩展的开发者,mysqli无论是面向对象还是面向过程的 方式都是非常容易理解的,有种似曾相识的感觉。具体的属性方法请参照php官方手册,http://php.net/manual/zh/mysqli.summary.php,下面我我通过一段代码示例mysqli的使用过程。
本例中操作做的表为test表,有id,title两个字段。
<?php //配置文件完成相关配置 define("HOST", "localhost"); define("USER", 'root'); define("PWD", ''); define("DB", 'yii'); //建立连接,生成mysqli实例对象。 $mysqli=new Mysqli(HOST,USER,PWD,DB); if ($mysqli->connect_errno) { "Connect Error:".$mysqli->connect_error; } //设置默认的字符集 $mysqli->set_charset('utf8'); $sql="select * from test"; //生成mysql_result对象 $result=$mysqli->query($sql); //返回二维关联数组,参数同理可以设定为MYSQLI_NUM返回索引数组,或者MYSQLI_BOTH二者兼有。 $rows=$result->fetch_all(MYSQLI_ASSOC); //将结果指针调整到任意行 $result->data_seek(2); $row=$result->fetch_row(); //$row=$result->fetch_array(); //$row=$result->fetch_assoc(); //$row=$result->fetch_object(); //释放结果集 $result->free(); //$result->free_result(); //$result->close(); //关闭连接 $mysqli->close();
以上代码仅仅是简单地展现了如何 利用mysqli进行查询,未对查询结果集进行遍历,如何取出数组中的数据应该不是难事。
需要注意的是$mysqli->query()执行的sql语句,成功执行SELECT, SHOW, DESCRIBE或 EXPLAIN查询会返回一个mysqli_result 对象,其他查询则返回 TRUE,执行失败则都返回false。
在进行INSERT,UPDATE,DELETE操作时可以调用$mysqli->affected_rows获得受影响记录条数
$mysqli->affected_rows 返回值 返回-1表明sql语句出现问题,0表示没有受影响的记录,其他数值则是受影响条数。
执行多条SQL语句,预处理,以及事务处理也是mysqli的重要内容,我会在后来的随笔中写到。
以上所述就是本文的全部内容了,希望大家能够喜欢。

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

AI Hentai Generator
Generate AI Hentai for free.

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

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

MySQL is an open source relational database management system that is widely used in web application development and data storage. Learning MySQL's SQL language is very necessary for data managers and developers. SQL language is the core part of MySQL, so before learning MySQL, you need to have a full understanding of SQL language. This article aims to explain the basic knowledge of SQL statements to you in detail, so that you can understand SQL statements step by step. SQL is the abbreviation of Structured Query Language, which is used in relational data
