Five common PHP Database issues (1) _php tutorial

Source: Internet
Author: User
If there is only one way to use Databaseis correct, there are many ways you can create database design, database access, and database-based PHPBusiness logic code, but in the end it usually ends in error. This article explains the five common problems that occur in the database design and the PHP code that accesses the database, and how to fix them when they encounter these problems.

Issue 1: Using MySQL directly

A common problem is that older PHP code uses the MYSQL_ function directly to access the database. Listing 1 shows how to access the database directly.

Listing 1. access/get.php

 
  1. function get_user_id ( $name )
  2. {
  3. $db = mysql_connect ( ' localhost ', ' root ', ' password ' );
  4. mysql_select_db ( ' users ' );
  5. $res = mysql_query ( "SELECT ID from Users WHERE login= '". $name . "'" );
  6. while ( $row = mysql_fetch_array ( $res )) { $id = $row[0];}
  7. return $id;
  8. }
  9. Var_dump (get_user_id ( ' Jack ' ));
  10. ?>

Note The Mysql_connect function is used to access the database. Also note the query, which uses a string connection to add $name parameters to the query.

There are two good alternatives to this technique: the PEAR DB module and the PHP Data Objects (PDO) class. Both provide abstraction from a specific database selection. As a result, your code doesn't need to be too much tuned to run on ibm®db2®, MySQL, PostgreSQL, or any other database you want to connect to.

Another value of using the PEAR DB module and the PDO abstraction layer is that you can use it in SQL statements. Operator. Doing so makes SQL easier to maintain and can protect your application from SQL injection attacks.

The alternative code that uses PEAR DB is shown below.

Listing 2. access/get_good.php

 
  1. require_once ("db.php");
  2. function get_user_id ( $name )
  3. {
  4. $dsn = ' Mysql://root:password@localhost/users ' ;
  5. $db =& db::connect ( $dsn, array());
  6. if (Pear::iserror ($db)) { die($db GetMessage ()); }
  7. $res = $db ->query ( ' SELECT ID from users WHERE login=? ' ) ,
  8. Array ( $name ));
  9. $id = null;
  10. while ( $res->fetchinto ( $row )) { $id = $row [0];}
  11. return $id;
  12. }
  13. Var_dump (get_user_id ( ' Jack ' ));
  14. ?>

Note that all the places that are used directly to MySQL are eliminated, except for the database connection string in the $dsn. In addition, we pass? operator to use $name variable in SQL. The queried data is then sent in through an array at the end of the query () method.

1

http://www.bkjia.com/PHPjc/445827.html www.bkjia.com true http://www.bkjia.com/PHPjc/445827.html techarticle if only one way to use the database is correct, you can create database design, database access, and database-based PHP business logic code in a number of ways, but ultimately ...

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