亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

實(shí)例講解php數(shù)據(jù)訪問

 更新時(shí)間:2016年05月09日 08:47:33   作者:陌上初薰  
這篇文章主要以實(shí)例講解的方式為大家詳細(xì)介紹了php數(shù)據(jù)訪問,數(shù)據(jù)訪問有兩種方式,本文為大家揭曉,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了兩種php數(shù)據(jù)訪問方式,大家可以進(jìn)行比較,分析兩種方式的異同,最后為大家提供了一個(gè)小練習(xí),具體內(nèi)容如下

方式一:已過時(shí),只做了解

1.造一個(gè)連接(建立通道)

$db=mysql_connect("localhost","root","123");     //括號(hào)內(nèi)是“服務(wù)器地址”,“用戶名”,“密碼”

2.選擇操作哪個(gè)數(shù)據(jù)庫

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

3.寫sql語句

$sql="select * from Info";

4.執(zhí)行sql語句

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

5.從結(jié)果集($result)中取數(shù)據(jù)

$row=mysql_fetch_row($result);  //每執(zhí)行一次讀取一行數(shù)據(jù)

$row1=mysql_fentch_row($result);  //執(zhí)行第二條數(shù)據(jù)

var_dump($row);

//讀取全部數(shù)據(jù)用循環(huán):

while($row=mysql_fetch_row($result))

{

  var_dump($row);  

}

方法二:面向?qū)ο?/p>

1.造一個(gè)連接對(duì)象:

$db=new MySQLi("localhost","root","123","mydb")  //括號(hào)內(nèi)的內(nèi)容依次為“服務(wù)器地址”,“用戶名”,“密碼”,“數(shù)據(jù)庫名稱”

2.判斷連接是否出錯(cuò):

2.1 mysqli_connect_error();  //代表連接出錯(cuò)

2.2

if(mysqli_connect_erroe())

       {

    echo "連接失敗!";

    exit();  //退出程序

        }

  2.3 !mysqli_connect_error or die ("連接失?。?); //“or”前面代表連接正確,后面代表連接失敗

3. 寫sql語句:

$sql="select * from nation";

4. 執(zhí)行sql語句:如果執(zhí)行成功返回結(jié)果集對(duì)象,如果執(zhí)行失敗返回false

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

5.從結(jié)果集中讀取數(shù)據(jù),先判斷是否有數(shù)據(jù)

if($result)

{

  //返回一行數(shù)據(jù)的索引數(shù)組,每次執(zhí)行返回一條數(shù)據(jù)

   var_dump($result->fetch_row()); 

  while($row=$result->fetch_row)

  {

    var_dump($row);

  }

  //返回一行數(shù)據(jù)的關(guān)聯(lián)數(shù)組,每次執(zhí)行返回一條數(shù)據(jù)

  var_dump($result->fetch_row()); 

  //通過二維數(shù)組返回所有數(shù)據(jù)

  var_dump($result->fetch_all());

  //以對(duì)象的方式返回一行數(shù)據(jù)

  var_dump($result->fetch_object());

}

練習(xí):

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>代號(hào)</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循環(huán)

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

以上就是本文的全部內(nèi)容,希望對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論