相信原来在开始学习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的重要内容,我会在后来的随笔中写到。
以上所述就是本文的全部内容了,希望大家能够喜欢。

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.


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

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment