php以mysqli的方法连接mysql数据库

随着php新版本的出现,用mysql_connect连接mysql数据库的方法逐渐弃用,改为了mysqli_connect,以下是mysqli_connect连接数据库的方法:

$conn = mysqli_connect('localhost', 'root', '', 'addressBook');//连接到数据库
//mysqli_query("set name 'utf-8'");
$sql = "select * from user";
$result = $conn->query($sql);

if ($result) 
{
	if ($result->num_rows>0)
	{
		while ($rows = $result->fetch_array()) {
			print_r($rows);
			echo "
rows['id']:".$rows['id']; echo "
rows['name']:".$rows['name']; echo "
rows['pwd']:".$rows['pwd']; }//end while() }else{ echo "
查询结果为空!"; }//end if() }else{ echo "
查询失败!"; }//end if()


你可能感兴趣的:(php)