Home > php教程 > php手册 > PHP5操作MySQL数据库

PHP5操作MySQL数据库

WBOY
Release: 2016-06-13 11:38:10
Original
1103 people have browsed it

1. 建立数据库连接

$mysqli = new mysqli("localhost","root","","mydb");
?>
建立一个数据库连接需要四个参数,分别为数据库地址、数据库访问用户名、数据库访问密码、数据库名称。除了使用上面的mysqli对象的构造方法建立数据库连接外,还可以调用其connect方法建立数据库的连接。

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

 

还可以通过mysqli对象的构造方法建立数据连接,通过select_db方法指定要访问的数据库。

$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
?>
通过mysqli对象的errno属性获取当前连接的错误号,如果当前连接没有任何错误,错误号返回为0。

$mysqli = new mysqli("localhost","root","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{

}
else
{
echo "The Connection is Error!";
exit();
}
?>
当然可以通过mysqli对象的error属性获取当前连接的错误信息,如果没有错误,返回“”。

$mysqli = new mysqli("localhost","rootsss","");
$mysqli->select_db("mydb");
if($mysqli->errno == 0) //判断当前连接是否成功
{

}
else
{
echo $mysqli->error; //输出当前错误信息
exit();
}
?>

  • 共5页:
  • 上一页
  • 1
  • 2
  • 3
  • 4
  • 5
  • 下一页
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template