PHP's mysqli extension is encapsulated in a class and is an object-oriented technology that executes faster and is more convenient and efficient than a traditional process approach
By default, Mysqli is not open in PHP and we need to
To use the mysqli extension in PHP, you need to add the following settings to the configuration file php.ini:
The code is as follows |
Copy Code |
Extension=php_mysqli.dll
|
If the above settings are already in the configuration file, make sure that there is no ";" in front of the extension, otherwise remove it. Here's how to use the mysqli extension to access the database.
The code is as follows |
Copy Code |
$db _host= "localhost"; The server address of the connection $db _user= "root"; User name of the connection database $db _psw= "root"; Password to connect to the database $db _name= "Sunyang"; Database name of the connection $mysqli =new mysqli (); $mysqli->connect ($db _host, $db _user, $db _psw, $db _name); ?>
|
Close the connection to the MySQL server by calling the close () method with the Mysqli object, for example:
$mysqli->close ();
The code is as follows |
Copy Code |
$connection = Mysqli_connect ("localhost", "root", "root", "Sunyang"); if ($connection) { echo "Database connection succeeded"; }else { echo "Database connection failed"; } ?> |
Data query
|
copy code |
"!--? php $mysqli =new mysqli ("Localho St "," root "," root "," Sunyang "); Instantiate mysqli $query = "SELECT * from employee"; $result = $mysqli->query ($query); if ($result) { if ($result->num_rows>0) {//Determines whether the number of rows in the result set is greater than 0 while ($row = $result->fetch_array ()) {/ /loop outputs the record in the result set Echo ($row [0]). " "; Echo ($row [1]). " "; Echo ($row [2]). " "; Echo ($row [3]). " "; echo ""; } } }else { echo "query failed"; } $result->free (); $mysqli->close (); ? |
Other like data save wait
Member methods in the Mysqli class
__construct (): Constructs a method for creating a Mysqli object, or you can establish a connection.
Autocommit (): Turns database modification autocommit on or off.
Change_user (): Changes the user specified by the database connection.
Character_set_name (): Returns the database connection default character set.
Close (): Closes the previously opened connection.
Commit (): commits the current thing.
Connect (): Open a new connection to the MySQL database server.
Debug (): Performs a debug operation.
Dump_debug_info (): Dumps debug information.
Get_client_info (): Returns the client version.
Get_host_info (): Returns a String representing the type of connection used, such as: Localhost via UNIX socket
Get_server_info (): Returns the MySQL server version.
Get_server_version (): Returns the MySQL server version in integer form.
Init (): Initializes a mysqli and returns a resource.
Info (): Retrieves information about the most recently executed query.
Kill (): kills a MySQL thread.
Multi_query (): Executes multiple query statements.
More_results (): Retrieves from the multi-query statement whether there are any more query results.
Next_result (): reads the next result from the currently executing multi-query.
Options (): Set option.
Ping (): Ping a server to connect or reconnect if there is no connection.
Prepare (): Prepares the execution of an SQL statement, returning the Mysqli_stmt object.
Query (): Interaction with the database is done through a query that sends a query to the database to execute, and the execution fails to return false.
Real_connect (): Attempt to open a connection to the MySQL database server.
Escape_string (): A string that escapes special characters.
Rollback (): Rolls back the current transaction.
select_db (): Select a default database for database queries.
Set_charset (): Sets the default client character set.
Ssl_set (): Use SSL to establish a secure connection.
Stat (): Gets the current system state.
Stmt_init (): Initializes a declaration that returns a Mysql_stmt object.
Store_result (): The result set is transferred from the last query.
Thread_safe (): Consider returning a secure thread.
member properties in the MySQL class
$affected _rows: The number of rows affected by the previous MySQL operation.
$client _info:mysql Client version (string).
$client _version:mysql Client version (integer).
$errno: The error code for the most recent function call.
$error: The error message string for the most recent function call.
$field _count (): Query Gets the number of columns.
$host _info: The connection type is used (string).
$info: The most recently executed query.
$insert _id: The last query automatically generated numbers.
$protocol the version used by the _version:mysql protocol.
$sqlstate: The last error that contains the SQLSTATE error code.
$thread _id: The current connection thread ID.
$warning _count: The number of warnings that were generated during the execution of the previous SQL statement.
http://www.bkjia.com/PHPjc/630740.html www.bkjia.com true http://www.bkjia.com/PHPjc/630740.html techarticle PHP's mysqli extension is encapsulated in a class, is an object-oriented technology, executes faster, is more convenient and more efficient than the traditional process method by default mysqli in PHP is ...