Introduction to specific functions of using MySQL directly in PHP

Source: Internet
Author: User

We are runningListing 1. Access/get. php

 
 
  1. <?php  
  2. function get_user_id( $name )  
  3. {  
  4.  $db = mysql_connect( 'localhost', 'root', 'password' );  
  5.  mysql_select_db( 'users' );  
  6.  
  7.  $res = mysql_query( "SELECT id FROM users WHERE login='".$name."'" );  
  8.  while( $row = mysql_fetch_array( $res ) ) { $id = $row[0]; }  
  9.  
  10.  return $id;  
  11. }  
  12.  
  13. var_dump( get_user_id( 'jack' ) );  
  14. ?> 

Note that the mysql_connect function is used for PHP to directly use MySQL. Pay attention to the query. Use string connection to add the $ name parameter to the query.

This technology has two good alternatives: the pear db module and the PHP Data Objects (PDO) class. Both provide abstraction from the choice of a specific database. Therefore, your code can be stored in IBM without too many adjustments? DB2? , MySQL, PostgreSQL, or any other database you want to connect.

Another value of using the pear db module and the PDO abstraction layer is that you can use it in SQL statements? Operator. This makes SQL easier to maintain and protects your applications from SQL injection attacks.

The alternative code for using pear db is as follows.

Listing 2. Access/get_good.php

 
 
  1. <?php  
  2. require_once("DB.php");  
  3.  
  4. function get_user_id( $name )  
  5. {  
  6.  $dsn = 'mysql://root:password@localhost/users';  
  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.  $id = null;  
  12.  while( $res->fetchInto( $row ) ) { $id = $row[0]; }  
  13.  
  14.  return $id;  
  15. }  
  16.  
  17. var_dump( get_user_id( 'jack' ) );  
  18. ?> 

Note that all PHP uses MySQL directly, except for the database connection strings in $ dsn. In addition, we pass? The operator uses the $ name variable in SQL. Then, the queried data is sent in through the array at the end of the query () method.


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.