php连接mysql...mysqli和mysql

  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);
}

 

 

-

posted @ 2015-11-05 18:56  根号五  阅读(332)  评论(0编辑  收藏  举报