PHP与数据库连接的方式有哪些?
2个回答
展开全部
1.最简单的方式
<?php$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_close($con);?>
2.面向对象mysqli(详细教程)
<?php$mysqli = new mysqli('localhost','root','','volunteer');if (mysqli_connect_errno()){ die('Unable to connect!'). mysqli_connect_error();}?>
3.pdo连接mysql(详细教程)
<?php $db = new PDO('mysql:host=localhost;dbname=test', 'root', '');try { foreach ($db->query('select * from user') as $row){ print_r($row); } $db = null; //关闭数据库} catch (PDOException $e) { echo $e->getMessage();}?>
4.ADODB连接mysql(详细教程)
<?phprequire_once './adodb5/adodb.inc.php';$conn = &ADONewConnection('mysql');$conn->connect('localhost','root','','test');$conn->Execute("set names utf8");$res = $conn->Execute("select * from user");if (!$res){ echo $conn->ErrorMsg();}else{ var_dump($res);}?>
<?php$con = mysql_connect("localhost","root","");if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_close($con);?>
2.面向对象mysqli(详细教程)
<?php$mysqli = new mysqli('localhost','root','','volunteer');if (mysqli_connect_errno()){ die('Unable to connect!'). mysqli_connect_error();}?>
3.pdo连接mysql(详细教程)
<?php $db = new PDO('mysql:host=localhost;dbname=test', 'root', '');try { foreach ($db->query('select * from user') as $row){ print_r($row); } $db = null; //关闭数据库} catch (PDOException $e) { echo $e->getMessage();}?>
4.ADODB连接mysql(详细教程)
<?phprequire_once './adodb5/adodb.inc.php';$conn = &ADONewConnection('mysql');$conn->connect('localhost','root','','test');$conn->Execute("set names utf8");$res = $conn->Execute("select * from user");if (!$res){ echo $conn->ErrorMsg();}else{ var_dump($res);}?>
网易云信
2023-12-06 广告
2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |