MYSQLI Basic Knowledge _mysql

Source: Internet
Author: User
Tags deprecated learn php

Believe that the original in the beginning to learn PHP, many people use the database preferred MySQL, the expansion of the database to connect the preferred MySQL expansion, but with the increase in PHP version, MySQL expansion is gradually replaced by mysqli and PDO. As with the MySQL function, the deprecated:the MySQL extension is deprecated and'll be removed in the Future:use mysqli or PDO instead. It is imperative to learn mysqli expansion.

Compared to MySQL extensions, mysqli extensions support object-oriented and process-oriented approaches, support preprocessing, support transactions, and speed faster than MySQL. This article will mainly introduce the mysqli of object-oriented Basic simple operation.

MYSQLI installation Configuration

MYSQLI installation configuration and other configuration, the first to make sure that your Ext folder exists Php_mysqli.dll files (generally exist), and in the php.ini file to remove the "Extension=php_mysqli.dll" before the ";" and determines the location of the extension_dir= ' ext directory in the configuration file. After restarting the server, you can use the mysqli extension ~

How do I verify that the mysqli extension is open?

The most straightforward thing is to use the mysqli extended function to see if it can be used, for example, by connecting to the database to determine if the extension has been installed. Connection success, needless to say, naturally is installed, the connection is not successful, do not easily think that not installed, we have after the recruit, using the phpinfo () function, we can clearly know mysqli is available.

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

The use of object-oriented mysqli

For developers who have used MySQL extensions, mysqli is very easy to understand, both object-oriented and process-oriented, with a sense of déjà vu. For specific property methods please refer to the official PHP manual, http://php.net/manual/zh/mysqli.summary.php, below me through a code example mysqli the use of the process.

The table in this example is the test table with Id,title two fields.

<?php 
//config file to complete the relevant configuration
define ("HOST", "localhost");
Define ("USER", ' root ');
Define ("PWD", "");
Define ("DB", ' Yii ');

Establishes a connection and generates a Mysqli instance object.
$mysqli =new mysqli (host,user,pwd,db);

if ($mysqli->connect_errno) {
  "Connect Error:". $mysqli->connect_error;
}
Sets the default character set
$mysqli->set_charset (' UTF8 ');
$sql = "SELECT * from Test";
Generate Mysql_result object
$result = $mysqli->query ($sql);

Returns a two-dimensional associative array in which the arguments can be set to Mysqli_num return an indexed array, or mysqli_both both.
$rows = $result->fetch_all (MYSQLI_ASSOC);
Adjust the result pointer to any row
$result->data_seek (2);
$row = $result->fetch_row ();
$row = $result->fetch_array ();
$row = $result->fetch_assoc ();
$row = $result->fetch_object ();

Release the result set
$result->free ();
$result->free_result ();
$result->close ();

Close the connection
$mysqli->close ();

The above code simply shows how to use mysqli to query, does not traverse the query result set, how to remove the data in the array should not be difficult.

Note that the SQL statement executed by $mysqli->query () succeeds in executing a SELECT, show, describe, or explain query that returns a Mysqli_result object, and other queries return TRUE. All returns False if the execution fails.

You can call $mysqli->affected_rows to get the number of affected records in the insert,update,delete operation

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

Executing multiple SQL statements, preprocessing, and transaction processing is also an important part of mysqli, which I will write in later essays.

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.