php 簡單資料庫連接類

來源:互聯網
上載者:User
  1. <?php   
  2. dd = new LampDb('localhost','root','2184237','note','utf8');   
  3. $sql = "select * from mynote";   
  4. $handle = $dd->query($sql);   
  5. $aa = $dd->fetchOneRow($handle);   
  6. print_r($aa);   
  7. echo "<hr />";   
  8. $ddd = $dd->fetchRows($handle);   
  9. for ($n=0; $n < count($ddd); $n++){   
  10. echo $ddd[$n]['id'] . "標題" . $ddd[$n]['subject'];   
  11. echo "<br />";   
  12. }   
  13. echo "<hr />";   
  14. echo "nums=" . $dd->fetchRowNums($handle);   
  15. ?>  
  16.  
  17. php   
  18. class LampDb   
  19. {   
  20. private $conn;   
  21. //串連資料庫   
  22. function __construct($host,$user,$pass,$dbname,$charset)   
  23. {   
  24.   $this->conn = mysql_connect($host,$user,$pass);   
  25.   mysql_query('set names $charset');   
  26.   $db = mysql_select_db($dbname,$this->conn);   
  27. }   
  28. //查詢語句   
  29. function query($sql)   
  30. {   
  31.   $handle = mysql_query($sql,$this->conn);   
  32.   return $handle;   
  33. }   
  34. //查詢一條記錄   
  35. function fetchOneRow($handle)   
  36. {   
  37.   $aa = mysql_fetch_assoc($handle);   
  38.   return $aa;   
  39. }   
  40. //查詢多條記錄   
  41. function fetchRows($handle,$i=0)   
  42. {   
  43.   while($bb = mysql_fetch_assoc($handle)){   
  44.    $bbb[$i] = $bb;   
  45.    $i++;   
  46.   }   
  47.   return $bbb;   
  48. }   
  49. //擷取一個查詢語句返回的記錄數   
  50. function fetchRowNums($handle)   
  51. {   
  52.   //$handle = $this->query($sql);   
  53.   $num = mysql_num_rows($handle);   
  54.   return $num;   
  55. }   
  56. }   
  57. ?>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.