How to connect to a database after PHP opens the mysqli extension _mysql

Source: Internet
Author: User

Mysqli is a feature that is only available after php5, and friends who do not have extensions open up your php.ini configuration file.
Look for the following statement:; Extension=php_mysqli.dll modify it to: Extension=php_mysqli.dll.
There are many new features and advantages relative to MySQL
(1) Support for local binding, preparation (prepare) and other syntax
(2) Error code to execute SQL statement
(3) Simultaneous execution of multiple SQL
(4) The method of object-oriented calling interface is also provided.
Below one by one uses the PHP instance to carry on the MYSQLI database connection!
Use method One: using traditional process-oriented approach
The PHP code is as follows:

Copy Code code as follows:

<?php
$connect = Mysqli_connect (' localhost ', ' root ', ', ', ' volunteer ') or Die (' Unale to connect ');
$sql = "SELECT * from Vol_msg";
$result = Mysqli_query ($connect, $sql);
while ($row = Mysqli_fetch_row ($result)) {
echo $row [0];
}
?>

Use method Two: Invoke an interface using an object-oriented method (recommended)
Look at the PHP code as follows:
Copy Code code as follows:

<?php
Create the object and open the connection, and the last parameter is the selected database name
$mysqli = new mysqli (' localhost ', ' root ', ' ', ' volunteer ');
Check if the connection was successful
if (Mysqli_connect_errno ()) {
Note Mysqli_connect_error () new features
Die (' Unable to connect! '). Mysqli_connect_error ();
}
$sql = "SELECT * from Vol_msg";
Execute the SQL statement, fully object-oriented
$result = $mysqli->query ($sql);
while ($row = $result->fetch_array ()) {
echo $row [0];
}
?>

The above two PHP instances run the same results, you can clearly see the advantages of using the Mysqli class object to build a database connection!
Insert and modify records I don't have to say, just change the SQL statement on the line, the next one I will speak prepare interface features!

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.