執行個體講解php資料訪問_php執行個體

來源:互聯網
上載者:User

本文執行個體為大家分享了兩種php資料訪問方式,大家可以進行比較,分析兩種方式的異同,最後為大家提供了一個小練習,具體內容如下

方式一:已淘汰,只做瞭解

1.造一個串連(建立通道)

$db=mysql_connect("localhost","root","123");     //括弧內是“伺服器位址”,“使用者名稱”,“密碼”

2.選擇操作哪個資料庫

mysql_select_db("mydb","$db");

3.寫sql語句

$sql="select * from Info";

4.執行sql語句

$result=mysql_query($sql);      //query 有查詢之意

5.從結果集($result)中取資料

$row=mysql_fetch_row($result);  //每執行一次讀取一行資料$row1=mysql_fentch_row($result);  //執行第二條資料var_dump($row);//讀取全部資料用迴圈:while($row=mysql_fetch_row($result)){  var_dump($row);  }

方法二:物件導向

1.造一個連線物件:

$db=new MySQLi("localhost","root","123","mydb")  //括弧內的內容依次為“伺服器位址”,“使用者名稱”,“密碼”,“資料庫名稱”

2.判斷串連是否出錯:

2.1 mysqli_connect_error();  //代表串連出錯

2.2

if(mysqli_connect_erroe())

       {

    echo "串連失敗!";

    exit();  //退出程式

        }

  2.3 !mysqli_connect_error or die ("串連失敗!"); //“or”前面代表串連正確,後面代表串連失敗

3. 寫sql語句:

$sql="select * from nation";

4. 執行sql語句:如果執行成功返回結果集對象,如果執行失敗返回false

$result=$db->query($sql);

5.從結果集中讀取資料,先判斷是否有資料

if($result){  //返回一行資料的索引數組,每次執行返回一條資料   var_dump($result->fetch_row());   while($row=$result->fetch_row)  {    var_dump($row);  }  //返回一行資料的關聯陣列,每次執行返回一條資料  var_dump($result->fetch_row());   //通過二維數組返回所有資料  var_dump($result->fetch_all());  //以對象的方式返回一行資料  var_dump($result->fetch_object());}

練習:

1.以下拉式功能表的形式在頁面顯示nation表

$db=new MySQLi("localhost","root","","mydb");!mysqli_connection_erroe() or die ("串連失敗!");$sql="select*from nation";$result=$db->query($sql);if($result){  $att=$result->fetch_all();  echo "<select>";  foreach ($att as $value)  {    echo "<option value='{$value[0]}'>{$value[1]}</option>";  }  echo "</select>";}
 

 

2. 把Info表查出來,以表格的形式顯示

$db=new MySQLi("localhost","root","","mydb");!mysqli_connecton_error() or die("串連失敗!");$sql="select * from info";$result=$bd->query($sql);if($result){$att=$result->fetch_all();echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";echo "<tr><td>代號</td><td>姓名</td><td>性別</td><td>民族</td><td>生日</td></tr>";foreach ($att as $value){ echo "<tr><td>{$value[0]}</td><td>{$value[1]}</td><td>{$value[2]}</td><td>{$value[3]}</td><td>{$value[4]}</td></tr>";}echo "</table>";} //也可以用for迴圈if($result){  $arr=$result->fetch_all();  echo "<table border='1' width='100%' cellpadding='0' cellspacing='0'>";  echo "<tr><td>Code</td><td>Name</td><td>Sex</td><td>Nation</td><td>Birthday</td></tr>";  for($i=0;$i<count($arr);$i++)  {    echo "<tr>    <td>{$arr[$i][0]}</td>    <td>{$arr[$i][1]}</td>    <td>{$arr[$i][2]}</td>    <td>{$arr[$i][3]}</td>    <td>{$arr[$i][4]}</td>     </tr>";  }  echo "</table>";}

以上就是本文的全部內容,希望對大家學習php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.