Quickly learn how to use PHP database files

Source: Internet
Author: User

PHP is still quite common, So I studied PHP database files and shared them with you here, hoping to be useful to you. We can see that in an application, each table is in a separate database. It is reasonable to do this in a very large database, but for general applications, this level of separation is not required. In addition, you cannot execute relational queries across databases, which affects the overall idea of using relational databases, not to mention the difficulty of managing tables across multiple databases. So what should multiple databases look like? First, you need some data. Listing 1 shows the data that is divided into four files.

Listing 1. PHP database file

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

You can create database design, database access, and database-based PHP business logic code in many ways, but it generally ends with errors and how to fix these problems. In the multi-database version of these files, you should load the SQL statement into one database and then load the users SQL statement into another database. The PHP code used to query files associated with a specific user in the database is as follows. The get_user function connects to the database containing the user table and retrieves the ID of the given user. The get_files function connects to the file table and retrieves the file rows associated with the specified user. A better way to do all of these things is to load the data into a database and then execute the query, such as the following query.

Listing 2. PHP database file Getfiles. php

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

Listing 3. Getfiles_good.php

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

This code is not only shorter, but also easier to understand and efficient. We do not execute two queries, but execute one query. Although this problem may sound far-fetched, in practice, we usually sum up that all the tables should be in the same database unless there is a very pressing reason.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.