search

I believe that when I started learning PHP, MySQL was the first choice for database used by many people, and MySQL extension was the first choice for extension to connect to the database. However, with the improvement of PHP version, MySQL extension is gradually being replaced by MySQL and PDO. As given when using mysql functions deprecated: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead. Learning mysqli extension is imperative.

  Compared with mysql extension, mysqli extension supports both object-oriented and process-oriented methods, supports pre-processing, supports transaction processing, and is faster than mysql. This article will mainly introduce the basic simple object-oriented operations of mysqli.

  Mysqli installation configuration

The installation configuration of mysqli is the same as other configurations. First, make sure that the php_mysqli.dll file exists in your ext folder (generally speaking, it exists), and in the php.ini file Remove the ";" before the line "extension=php_mysqli.dll", and make sure that extension/span>

in the configuration file. How to verify that the mysqli extension has been turned on?

 In fact, the most direct way is to use the function of the mysqli extension to see if it can be used. For example, you can judge whether the extension has been installed by whether it can connect to the database. Needless to say, if the connection is successful, it is naturally installed. If the connection is unsuccessful, don't think that it is not installed. We have a backup plan. Using the phpinfo() function, we can clearly know whether mysqli is available.

 Of course, you can use extension_loaded('mysqli') to determine whether the mysqli extension is loaded, and you can even use get_loaded_extensions() to get which extensions are loaded.

The use of object-oriented mysqli

For developers who have used mysql extensions, mysqli is very easy to understand whether it is object-oriented or process-oriented, and it feels like deja vu. For specific attribute methods, please refer to the official PHP manual, http://php.net/manual/zh/mysqli.summary.php. Below I will illustrate the use of mysqli through a piece of code.

In this example, the table used for the operation is the test table, which has two fields: id and title.

<span><span>php 
</span><span>//</span><span>配置文件完成相关配置</span><span>define</span>("HOST", "localhost"<span>);
</span><span>define</span>("USER", 'root'<span>);
</span><span>define</span>("PWD", ''<span>);
</span><span>define</span>("DB", 'yii'<span>);

</span><span>//</span><span>建立连接,生成mysqli实例对象。</span><span>$mysqli</span>=<span>new</span> Mysqli(HOST,USER,PWD,<span>DB);

</span><span>if</span> (<span>$mysqli</span>-><span>connect_errno) {
    </span>"Connect Error:".<span>$mysqli</span>-><span>connect_error;
}
</span><span>//</span><span>设置默认的字符集</span><span>$mysqli</span>->set_charset('utf8'<span>);
</span><span>$sql</span>="select * from test"<span>;
</span><span>//</span><span>生成mysql_result对象</span><span>$result</span>=<span>$mysqli</span>->query(<span>$sql</span><span>);

</span><span>//</span><span>返回二维关联数组,参数同理可以设定为MYSQLI_NUM返回索引数组,或者MYSQLI_BOTH二者兼有。</span><span>$rows</span>=<span>$result</span>-><span>fetch_all(MYSQLI_ASSOC);


</span><span>//</span><span>将结果指针调整到任意行</span><span>$result</span>->data_seek(2<span>);
</span><span>$row</span>=<span>$result</span>-><span>fetch_row();
</span><span>//</span><span>$row=$result->fetch_array();
//$row=$result->fetch_assoc();
//$row=$result->fetch_object();

//释放结果集</span><span>$result</span>-><span>free();
</span><span>//</span><span>$result->free_result();
//$result->close();

//关闭连接</span><span>$mysqli</span>->close();</span>

The above code simply shows how to use mysqli to query without traversing the query result set. It should not be difficult to retrieve the data in the array.

It should be noted that the sql statement executed by $mysqli->query() will return a mysqli_result object if the query is successfully executed SELECT, SHOW, DESCRIBE or EXPLAIN, and other queries will return TRUE. If the execution fails, false will be returned.

You can call $mysqli->affected_rows to get the number of affected records when performing INSERT, UPDATE, and DELETE operations.

$mysqli->affected_rows The return value of -1 indicates that there is a problem with the sql statement, and 0 means that there is a problem with the sql statement. There are no affected records, and other values ​​are the number of affected records.

Executing multiple SQL statements, preprocessing, and transaction processing are also important aspects of mysqli, which I will write about in a later essay.

ps: Is this my first time writing a technical blog so nervous? There are many inappropriate wordings and I still hope you can give me more opinions~

The above has introduced the basic knowledge of Mysqli, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Fatal error: Call to undefined function mysqli_connect()的解决方法PHP Fatal error: Call to undefined function mysqli_connect()的解决方法Jun 23, 2023 am 09:40 AM

在使用PHP编写Web应用程序时,经常会使用MySQL数据库来存储数据。PHP提供了一种与MySQL数据库进行交互的方法,称为MySQLi。然而,有时在使用MySQLi时,会遇到一个错误信息,如以下所示:PHPFatalerror:Calltoundefinedfunctionmysqli_connect()这个错误信息意味着PHP无法找到my

mysql怎么替换换行符mysql怎么替换换行符Apr 18, 2022 pm 03:14 PM

在mysql中,可以利用char()和REPLACE()函数来替换换行符;REPLACE()函数可以用新字符串替换列中的换行符,而换行符可使用“char(13)”来表示,语法为“replace(字段名,char(13),'新字符串') ”。

php无法连接mysqli怎么办php无法连接mysqli怎么办Nov 09, 2022 am 10:07 AM

php无法连接mysqli的解决办法:1、打开“php.ini”文件;2、找到“mysqli.reconnect”;3、将“mysqli.reconnect = OFF”改成“mysqli.reconnect = on”即可。

PHP PDO 与 mysqli:比较和对比PHP PDO 与 mysqli:比较和对比Feb 19, 2024 pm 12:24 PM

PDOPDO是一个面向对象的数据库访问抽象层,它为PHP提供了一个统一的接口,允许您使用相同的代码与不同的数据库(如Mysql、postgresql、oracle)进行交互。PDO隐藏了底层数据库连接的复杂性,简化了数据库操作。优缺点优点:统一接口,支持多种数据库简化数据库操作,降低开发难度提供预处理语句,提高安全性支持事务处理缺点:性能可能比原生扩展稍低依赖外部库,可能会增加开销演示代码使用PDO连接mysql数据库:$db=newPDO("mysql:host=localhost;dbnam

mysql怎么将varchar转换为int类型mysql怎么将varchar转换为int类型May 12, 2022 pm 04:51 PM

转换方法:1、利用cast函数,语法“select * from 表名 order by cast(字段名 as SIGNED)”;2、利用“select * from 表名 order by CONVERT(字段名,SIGNED)”语句。

mysql的运行文件是什么mysql的运行文件是什么Apr 11, 2023 am 10:38 AM

mysql的运行文件是mysqld;mysqld是一个可执行文件,代表着Mysql服务器程序,执行这个文件可以直接启动一个服务器进程;而mysqld_safe是一个启动脚本,它会间接调用mysqld,并且还会顺带启动一个监控进程。

PHP Warning: mysqli_connect(): (HY000/2002): Connection refused的解决方法PHP Warning: mysqli_connect(): (HY000/2002): Connection refused的解决方法Jun 23, 2023 am 08:54 AM

如果你使用PHP连接MySQL数据库时遇到了以下错误提示:PHPWarning:mysqli_connect():(HY000/2002):Connectionrefused那么你可以尝试按照下面的步骤来解决这个问题。确认MySQL服务是否正常运行首先应该检查MySQL服务是否正常运行,如果服务未运行或者启动失败,就可能会导致连接被拒绝的错误。你可

MySQL复制技术之异步复制和半同步复制MySQL复制技术之异步复制和半同步复制Apr 25, 2022 pm 07:21 PM

本篇文章给大家带来了关于mysql的相关知识,其中主要介绍了关于MySQL复制技术的相关问题,包括了异步复制、半同步复制等等内容,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool