php示例代码之使用mysqli对象

<?php 
  //连接参数
  $host="localhost";
  $user="root";
  $pwd="111111";
  $db="test";
  
  $mysqli =new mysqli($host,$user,$pwd,$db); //实例化一个mysqli对象,并打开一个连接
  
  if(mysqli_connect_errno()){ //检查是否可以正确打开数据库
  	echo "<font color='red'>unable to connect!</font>";
  }
  
  $SQL_SELECT_SYMBOLS="select * from symbols"; 
  
  if($result=$mysqli->query($SQL_SELECT_SYMBOLS)){
  	 if($result->num_rows>0){
  	 	
  	 	echo '<table cellpadding="10" border="1">';
  	    echo '<tr><th>id</th><th>country</th><th>animal</th><th>cname</th></tr>';
  	    while($arr=$result->fetch_array()){
	  	 	echo "<tr>";
	  	 	echo "<td>".$arr[0]."</td>";
	  	 	echo "<td>".$arr[1]."</td>";
	  	    echo "<td>".$arr[2]."</td>";
	  	    echo "<td>".$arr[3]."</td>";
	  	 	echo "</tr>";
  	    }
  	     echo "</table>";	
    }
   else{
  	echo "记录未找到!";
   }
  
  $result->close(); //释放记录集所占用的内存
  }
  else{
  	echo "error in query:$query.".$mysqli->error;
  }
  $mysqli->close(); //关闭数据库连接
?>

posted @ 2011-07-05 15:54  来自非洲大草原的食人虎  阅读(1796)  评论(0编辑  收藏  举报