快速學習PHP資料庫檔案使用_PHP教程

來源:互聯網
上載者:User
PHP還是比較常用的,於是我研究了一下PHP資料庫檔案,在這裡拿出來和大家分享一下,希望對大家有用。我們會看到一個應用程式中,每個表都在一個單獨的資料庫中。在大型資料庫中這樣做是合理的,但是對於一般的應用程式,則不需要這種層級的分割。此外,不能跨資料庫執行關係查詢,這會影響使用關聯式資料庫的整體思想,更不用說跨多個資料庫管理表會更困難了。 那麼,多個資料庫應該是什麼樣的呢?首先,您需要一些資料。清單 1 展示了分成 4 個檔案的這樣的資料。

清單 1. PHP資料庫檔案

 
  1. Files.sql:
  2. CREATE TABLE files (
  3. id MEDIUMINT,
  4. user_id MEDIUMINT,
  5. name TEXT,
  6. path TEXT
  7. );
  8. Load_files.sql:
  9. INSERT INTO files VALUES ( 1, 1, 'test1.jpg', 'files/test1.jpg' );
  10. INSERT INTO files VALUES ( 2, 1, 'test2.jpg', 'files/test2.jpg' );
  11. Users.sql:
  12. DROP TABLE IF EXISTS users;
  13. CREATE TABLE users (
  14. id MEDIUMINT,
  15. login TEXT,
  16. password TEXT
  17. );
  18. Load_users.sql:
  19. INSERT INTO users VALUES ( 1, 'jack', 'pass' );
  20. INSERT INTO users VALUES ( 2, 'jon', 'pass' );

您可以用很多的方式建立資料庫設計、資料庫訪問和基於資料庫的 PHP 商務邏輯代碼,但最終一般以錯誤告終,以及在遇到這些問題時如何修複它們。在這些檔案的多資料庫版本中,您應該將 SQL 陳述式載入到一個資料庫中,然後將 users SQL 陳述式載入到另一個資料庫中。用於在資料庫中查詢與某個特定使用者相關聯的檔案的 PHP 代碼如下所示。get_user 函數串連到包含使用者表的資料庫並檢索給定使用者的 ID。get_files 函數串連到檔案表並檢索與給定使用者相關聯的檔案行。做所有這些事情的一個更好辦法是將資料載入到一個資料庫中,然後執行查詢,比如下面的查詢。

清單 2. PHP資料庫檔案Getfiles.php

 
  1. <?php
  2. require_once("DB.php");
  3. function get_user( $name )
  4. {
  5. $dsn = 'mysql://root:password@localhost/bad_multi1';
  6. $db =& DB::Connect( $dsn, array() );
  7. if (PEAR::isError($db)) { die($db->getMessage()); }
  8. $res = $db->query( "SELECT id FROM users WHERE login=?",array( $name ) );
  9. $uid = null;
  10. while( $res->fetchInto( $row ) ) { $uid = $row[0]; }
  11. return $uid;
  12. }
  13. function get_files( $name )
  14. {
  15. $uid = get_user( $name );
  16. $rows = array();
  17. $dsn = 'mysql://root:password@localhost/bad_multi2';
  18. $db =& DB::Connect( $dsn, array() );
  19. if (PEAR::isError($db)) { die($db->getMessage()); }
  20. $res = $db->query( "SELECT * FROM files WHERE user_id=?",array( $uid ) );
  21. while( $res->fetchInto( $row ) ) { $rows[] = $row; }
  22. return $rows;
  23. }
  24. $files = get_files( 'jack' );
  25. var_dump( $files );
  26. ?>

清單 3. Getfiles_good.php

 
  1. <?php
  2. require_once("DB.php");
  3. function get_files( $name )
  4. {
  5. $rows = array();
  6. $dsn = 'mysql://root:password@localhost/good_multi';
  7. $db =& DB::Connect( $dsn, array() );
  8. if (PEAR::isError($db)) { die($db->getMessage()); }
  9. $res = $db->query("SELECT files.* FROM users, files WHERE
  10. users.login=? AND users.id=files.user_id",
  11. array( $name ) );
  12. while( $res->fetchInto( $row ) ) { $rows[] = $row; }
  13. return $rows;
  14. }
  15. $files = get_files( 'jack' );
  16. var_dump( $files );
  17. ?>

該代碼不僅更短,而且也更容易理解和高效。我們不是執行兩個查詢,而是執行一個查詢。儘管該問題聽起來有些牽強,但是在實踐中我們通常總結出所有的表應該在同一個資料庫中,除非有非常迫不得已的理由。


http://www.bkjia.com/PHPjc/446457.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446457.htmlTechArticlePHP還是比較常用的,於是我研究了一下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.