Mysqli basics

WBOY
Release: 2016-08-08 09:20:58
Original
810 people have browsed it

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>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template