继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

php连接mysql数据库的几种方法

慕粉0415482673
关注TA
已关注
手记 297
粉丝 64
获赞 367

1.最简单的方式

1<span style="font-family:'微软雅黑', 'Microsoft YaHei';font-size:14px;color:rgb(0,0,0);"><?php<br>$con = mysql_connect("localhost","root","");<br>if (!$con)<br> {<br>  die('Could not connect: ' . mysql_error());<br> }<br>mysql_close($con);<br>?><br></span>

2.面向对象mysqli

1<span style="font-family:'微软雅黑', 'Microsoft YaHei';font-size:14px;color:rgb(0,0,0);"><?php<br>$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');<br>try {<br> foreach ($db->query('select * from user') as $row){<br>     print_r($row);<br>  }<br>  $db = null;  //关闭数据库<br>} <br> catch (PDOException $e) {echo $e->getMessage();<br>}<br>?><br></span>

3.pdo连接mysql

1<span style="font-family:'微软雅黑', 'Microsoft YaHei';font-size:14px;color:rgb(0,0,0);"><?php<br>$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');<br>try {<br>    foreach ($db->query('select * from user') as $row){<br>        print_r($row);<br>    }<br>    $db = null;  //关闭数据库<br>} <br>catch (PDOException $e) {<br>    echo $e->getMessage();<br>}<br>?></span>

4.ADODB连接mysql

1<span style="font-family:'微软雅黑', 'Microsoft YaHei';font-size:14px;color:rgb(0,0,0);"><?php<br>require_once './adodb5/adodb.inc.php';<br>$conn = &ADONewConnection('mysql');<br>$conn->connect('localhost','root','','test');<br>$conn->Execute("set names utf8");<br>$res = $conn->Execute("select * from user");<br>if (!$res){<br>    echo $conn->ErrorMsg();<br>}else{<br>    var_dump($res);<br>}<br>?></span>



打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP