Php connection to mysql multiple methods and instance code-php Tutorial

Source: Internet
Author: User
Tags php database
Php connection to mysql multiple methods and instance code

  1. Mysql_connect ("localhost", "root", "1981427") // connect to the server on localhost. the username is root.
  2. ?>

Connect and select database

  1. @ Mysql_select_db ("test") // select the database mydb
  2. ?>

In this way, you can connect to the database.

The following code appears: call to undefined function 'MySQL _ connect ()'... The error message "function undefined" indicates that the mysql_connect () function does not exist,

Solution: set php_mysql.dll and libmysql. copy the dll file to c: winntsystem32 (I missed libmysql. dll) find php. in ini; extension = php_mysql, remove the previous ";" Restart Server example 1, conn. php file code:

  1. $ Conn = @ mysql_connect ("localhost", "root", "root") or die ("database link error ");
  2. Mysql_select_db ("data", $ conn); // data is the database name
  3. Mysql_query ("set names 'gbk'"); // use gbk Chinese encoding;
  4. ?>

Index. php file code:

  1. Include ("conn. php"); // introduce the conn. php file
  2. $ SQL = "select * from 'table' order by id desc ";
  3. $ Query = mysql_query ($ SQL );
  4. While ($ row = mysql_fetch_array ($ query )){
  5. ?>

Show data content:

  1. }
  2. ?>

Php connection to mysql database

Method 1: Common method (process-oriented)

First, the database user information:

  1. // Generate a connection

  2. $ Db_connect = mysql_connect ($ dbhost, $ username, $ userpass) or die ("unable to connect to the mysql! ");
  3. // Select a database to be operated
  4. Mysql_select_db ($ dbdatabase, $ db_connect );
  5. // Execute the mysql statement
  6. $ Result = mysql_query ("select id, name from user ");

  7. // Extract data

  8. $ Row = mysql_fetch_row ($ result );

Supplement: ① using @ (error control operator) before functions such as mysql_connect () and mysql_select_db () can ignore the error messages generated by the system, and then using die () customize error messages;

② In addition to mysql_fetch_row, mysql_fetch_assoc and mysql_fetch_array are commonly used for data extraction. for specific differences, see php manual;

③ For the return value of the mysql_query () function, if the executed statement has a return value (such as select, show, describe, etc.), the corresponding data (when successful) or false (when failed) is returned ); if the executed statement does not return values (such as delete, drop, insert, and update), true (when successful) or false (when failed) is returned ).

Method 2: php object-oriented method to connect to mysql database

In fact, this method is very similar to a common method, but the corresponding function is replaced with an object-oriented method, directly look at the code.

  1. $ Db = new mysqli ($ dbhost, $ username, $ userpass, $ dbdatabase );
  2. If (mysqli_connect_error ()){
  3. Echo 'could not connect to database .';
  4. Exit;
  5. }
  6. $ Result = $ db-> query ("select id, name from user ");
  7. $ Row = $ result-> fetch_row ();

Mysqli is used here, which refers to mysql extension. it can be used to interact with databases through process-oriented or object-oriented methods, the only difference is that the method for calling a function (object method) is different.

Method 3: connect to the mysql database using the php pdo method

Pdo is actually the abbreviation of php database objects. Chinese is the php database object. It provides a unified php-database interaction method.

This is a popular method for connecting to databases. Its advantage is that, as long as the data source is provided correctly, the basic operations on the database are the same. That is to say, the same code can interact with mysql, sqlite3, or postgresql, provided that you provide the correct data source. Code for connecting to mysql:

  1. $ Dsn = 'MySQL: host = '. $ dbhost.'; dbname = '. $ dbdatabase .';'

  2. $ Dbh = new pdo ($ dsn, $ username, $ userpass );

  3. Sqlite3:

  4. $ Dsn = 'sqlite3: "c: \ sqlite \ user. db "';
  5. $ Dbh = new pdo ($ dsn );

  6. Postgresql:

  7. $ Dsn = 'pgsql: host = '. $ dbhost. 'Port = 5432 dbname ='. $ dbdatabase. 'user = '. $ username. 'password ='. $ userpass;
  8. $ Dbh = new pdo ($ dsn );

After successfully establishing a connection with the database, you only need to obtain data from the database or insert the updated data.

SQL code:

  1. $ Stmt = $ dbh-> query ('select id, name from user ');
  2. $ Row = $ stmt-> fetch ();

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.