Home > Article > Backend Development > Mysqli basics, mysql_PHP tutorial
I believe that when they started to learn PHP, many people preferred MySQL as the database and MySQL extension as the extension to connect to the database, but then With the improvement of PHP version, mysql extension is gradually being replaced by mysqli 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 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.
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 it exists), and remove "extension=php_mysqli" in the php.ini file. .dll" before this line, and determine the location of the extension_dir='ext directory in the configuration file. After restarting the server, you can use the mysqli extension~
In fact, the most direct way is to use the function of 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.
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.
The table used in this example 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, other queries return
TRUE,执行失败则都返回false。
When performing INSERT, UPDATE, and DELETE operations, you can call $mysqli->affected_rows to obtain the number of affected records
$mysqli->affected_rows Return value Returning -1 indicates that there is a problem with the sql statement, 0 indicates that 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 it so nervous to write a technical blog for the first time? There are many inappropriate wordings and I hope everyone can give me your opinions~
http://www.bkjia.com/PHPjc/1031812.html
www.bkjia.com