本文实例讲述了php+mysqli数据库连接的两种方式。分享给大家供大家参考。具体如下:
这里讲述mysqli数据库连接两种方式比较,即面向对象与面向过程。代码如下:
第一种方式:面向对象的方式建立数据库连接
$mysqli = new MySQLi("localhost","root","1233456");//默认的 MySQL的类,其属性与方法见手册
if($mysqli->connect_error){//connect_error为属性,报错
die("数据库连接失败:".$mysqli->connect_errno."--".$mysqli->connect_error);// connect_errno:错误编号
}
$mysqli->select_db("liuyan");//选择数据库
$mysqli->query("set names 'GBK'");
第二种方式:面向过程方式建立数据库连接,这个与 mysql 扩展库类似
$conn = @mysqli_connect("localhost","root","123456") or die(mysqli_connect_error());
mysqli_select_db($conn,"liuyan") or die(mysqli_connect_error());//与mysql扩展库不同,这里数据库名写在后面,而且 $conn是必选的
mysqli_query($conn,"set names 'GBK'");//同上
?>
希望本文所述对大家的php程序设计有所帮助。
Statement: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