您的位置:首页 > 编程语言 > PHP开发

php连接mysql...mysqli和mysql

2015-11-05 18:56 501 查看
  mysql_connect()这一系列函数已经不推荐使用了,不安全。  

<?php

$con = mysql_connect('localhost','root','');// 选择连接数据库系统
if(!$con){
die('no con...' . mysql_error());
}
mysql_select_db('test', $con);// 选择哪个数据库

$sql = 'select * from hello limit 1,2';// sql
$re = mysql_query($sql, $con);// 查询,结果是个资源

while($row = mysql_fetch_array($re)){// mysql_fetch_assoc
print_r($row);
}


  现在推荐使用mysqli的系列函数。

<?php

// 参数分别是:主机名 用户名 密码 数据库名称 端口
$con = mysqli_connect('localhost','root','','test');// 这里已经包括数据库名称

$sql = 'select * from hello limit 1,2';
$re = mysqli_query($con, $sql);

while($row = mysqli_fetch_assoc($re)){
print_r($row);
}


-
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: