php+mysqli数据库连接的两种方式
作者:shichen2014 时间:2023-10-08 22:15:16
本文实例讲述了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程序设计有所帮助。
标签:php,mysqli,数据库
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在ASP.NET 2.0中操作数据之十九:给编辑和新增界面增加验证控件
2024-05-09 09:04:01
![](https://img.aspxhome.com/file/2023/9/129809_0s.png)
手把手教你在Python里使用ChatGPT
2023-11-10 02:55:58
![](https://img.aspxhome.com/file/2023/9/75339_0s.jpg)
Windows 下python3.8环境安装教程图文详解
2023-05-09 09:55:09
![](https://img.aspxhome.com/file/2023/4/118334_0s.jpg)
解决python opencv无法显示图片的问题
2021-09-11 13:26:37
![](https://img.aspxhome.com/file/2023/6/131126_0s.jpg)
asp查询ip地址源代码
2009-07-27 17:51:00
linux环境中没有网络怎么下载python
2021-01-30 04:42:12
实例:用 JavaScript 来操作字符串(一些字符串函数)
2023-06-30 10:02:21
python多线程编程方式分析示例详解
2023-05-13 04:56:01
Python实现的服务器示例小结【单进程、多进程、多线程、非阻塞式】
2023-02-24 00:19:25
利用python为PostgreSQL的表自动添加分区
2023-07-07 14:44:58
使用PyCharm官方中文语言包汉化PyCharm
2023-03-20 23:42:38
![](https://img.aspxhome.com/file/2023/7/100807_0s.png)
Windows64x下VScode下载过程
2021-03-08 20:10:47
![](https://img.aspxhome.com/file/2023/1/81441_0s.jpg)
MySQL修改innodb_data_file_path参数的一些注意事项
2024-01-20 14:32:23
利用XMLHTTP无刷新自动实时更新数据
2008-02-19 18:29:00
Pip install和Conda install的使用
2023-11-04 17:32:37
Python中Dict两种实现的原理详解
2022-11-13 06:20:29
golang API开发过程的中的自动重启方式(基于gin框架)
2024-02-03 02:56:48
python通过colorama模块在控制台输出彩色文字的方法
2023-07-23 00:35:51
pyinstaller 3.6版本通过pip安装失败的解决办法(推荐)
2023-10-13 11:55:44
![](https://img.aspxhome.com/file/2023/0/117580_0s.png)
python mac下安装虚拟环境的图文教程
2021-11-02 23:35:21
![](https://img.aspxhome.com/file/2023/2/82942_0s.jpg)