Quick Learning PHP database files using _php tutorial

Source: Internet
Author: User
PHP is still more commonly used, so I studied the PHP database file, here to take out and share with you, hope to be useful to everyone. We will see an application in which each table is in a separate database. It is reasonable to do so in a very large database, but for a generic application, this level of fragmentation is not required. In addition, relational queries cannot be executed across databases, which can affect 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 such data divided into 4 files.

Listing 1. PHP Database files

 
 
  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 ');

There are a number of ways you can create database design, database access, and database-based PHP business logic code, but ultimately end up with errors and how to fix them when you encounter these problems. In the multi-database version of these files, you should load the SQL statements into one database, and then load the users SQL statements into another database. The PHP code used to query the database for files associated with a particular user is shown below. The Get_user function connects to the database that contains the user table and retrieves the ID of the given user. The Get_files function connects to a file table and retrieves the file rows associated with a given 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. Require_once ("db.php");
  2. function Get_user ($name)
  3. {
  4. $ DSN = ' Mysql://root:password@localhost/bad_multi1 ' ;
  5. $ DB =& db::connect ($DSN, Array ());
  6. if (Pear::iserror ($db)) {die ($db->getmessage ());}
  7. $ Res = $db->query ("Select ID from users WHERE login=?", array ($name));
  8. $ UID = NULL ;
  9. While ($res->fetchinto ($row)) {$uid = $row [0];}
  10. return $uid;
  11. }
  12. function Get_files ($name)
  13. {
  14. $ UID = Get_user ($name);
  15. $ rows = Array ();
  16. $ DSN = ' Mysql://root:password@localhost/bad_multi2 ' ;
  17. $ DB =& db::connect ($DSN, Array ());
  18. if (Pear::iserror ($db)) {die ($db->getmessage ());}
  19. $ Res = $db->query ("select * from Files WHERE user_id=?", array ($uid));
  20. while ($res->fetchinto ($row)) {$rows [] = $row;}
  21. return $rows;
  22. }
  23. $ Files = Get_files (' Jack ');
  24. Var_dump ($files);
  25. ? >

Listing 3. getfiles_good.php

 
 
  1. Require_once ("db.php");
  2. function Get_files ($name)
  3. {
  4. $ rows = Array ();
  5. $ DSN = ' Mysql://root:password@localhost/good_multi ' ;
  6. $ DB =& db::connect ($DSN, Array ());
  7. if (Pear::iserror ($db)) {die ($db->getmessage ());}
  8. $ Res = $db->query ("Select files.* from Users, files WHERE
  9. Users.login =? and users.id=files. user_id ",
  10. Array ($name));
  11. while ($res->fetchinto ($row)) {$rows [] = $row;}
  12. return $rows;
  13. }
  14. $ Files = Get_files (' Jack ');
  15. Var_dump ($files);
  16. ? >

The code is not only shorter, but also easier to understand and efficient. Instead of executing two queries, we execute a query. Although the problem may sound far-fetched, in practice we usually conclude that all tables should be in the same database, unless there is a compelling reason to do so.


http://www.bkjia.com/PHPjc/446457.html www.bkjia.com true http://www.bkjia.com/PHPjc/446457.html techarticle PHP is still more commonly used, so I studied the PHP database file, here to take out and share with you, hope to be useful to everyone. We will see an application in which each ...

  • 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.