PHP5 operating MySQL database_PHP tutorial

WBOY
Release: 2016-07-21 14:55:44
Original
820 people have browsed it

1. Establish database connection

$mysqli = new mysqli("localhost","root","","mydb");
?>
Four parameters are required to establish a database connection. They are database address, database access user name, database access password, and database name. In addition to using the above constructor method of the mysqli object 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 establish a data connection through the construction method of the mysqli object, and specify the database to be accessed through the select_db method.

$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
?>
Get the error number of the current connection through the errno attribute of the mysqli object. If there are no errors in the current connection, the error number is returned to 0.

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

}
else
{
echo "The Connection is Error!";
exit();
}
?>
Of course, you can pass the mysqli object The error attribute obtains the error information of the current connection. If there is no error, returns "".

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

}
else
{
echo $mysqli->error; //Output the current error message
exit();
}
?>

  • Total 5 pages:
  • Previous page
  • 1
  • 2
  • 3
  • 4
  • 5
  • Next page

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364344.htmlTechArticle1. Establish a database connection?php $mysqli = new mysqli(localhost,root,,mydb); ? Create a database connection Database connection requires four parameters, namely database address, database access user name, data...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template