PHP5 tutorial on MySQL database _ PHP

Source: Internet
Author: User
PHP5 operates on the MySQL database. 1. create a database connection? Php $ mysqlinewmysqli (localhost, root, mydb );? To establish a database connection, four parameters are required: database address, database access username, and data. 1. create a database connection.

$ Mysqli = new mysqli ("localhost", "root", "", "mydb ");
?>
To establish a database connection, four parameters are required: database address, database access username, database access password, and database name. In addition to using the above mysqli object constructor to establish a database connection, you can also call its connect method to establish a database connection.

$ Mysqli = new mysqli ();
$ Mysqli-> connect ("localhost", "root", "", "mydb ");
?>

You can also use the mysqli object constructor to establish a data connection and use the select_db method to specify the database to be accessed.

$ Mysqli = new mysqli ("localhost", "root ","");
$ Mysqli-> select_db ("mydb ");
?>
Get the error code of the current connection through the errno attribute of the mysqli object. if there is no error in the current connection, the error code is returned as 0.

$ Mysqli = new mysqli ("localhost", "root ","");
$ Mysqli-> select_db ("mydb ");
If ($ mysqli-> errno = 0) // checks whether the current connection is successful.
{

}
Else
{
Echo "The Connection is Error! ";
Exit ();
}
?>
Of course, you can use the error attribute of the mysqli object to get the error message of the current connection. if there is no error, "" is returned.

$ Mysqli = new mysqli ("localhost", "rootsss ","");
$ Mysqli-> select_db ("mydb ");
If ($ mysqli-> errno = 0) // checks whether the current connection is successful.
{

}
Else
{
Echo $ mysqli-> error; // output the current error message
Exit ();
}
?>

  • 5 pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • Next page

Http://www.bkjia.com/PHPjc/364344.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364344.htmlTechArticle1. create database connection? Php $ mysqli = new mysqli (localhost, root, mydb );? To establish a database connection, four parameters are required: database address, database access username, and data...

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.