WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

php示例代码之使用mysqli对象

Posted on 2016-07-09 13:39  WebEnh  阅读(431)  评论(0编辑  收藏  举报
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?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(); //关闭数据库连接
?>