Example of a Select query statement in Php

Source: Internet
Author: User

Example of a Select query statement in Php

To query the content in the mysql database in php, we must first connect to the mysql database and then use the SQL statement for query. Let's take a look at some examples below.

SQL has many statements for database operations. However, common and comparative statements are required. SELECT statements are used to SELECT data from the database.

 

First, we will introduce the first SELECT statement.

Statement 1: SELECT * FROM table_name

Explanation: reads the data in the entire table table_name and displays it.

Statement 1: SELECT * FROM table_name Where x = 1

Explanation: Read the data in Table 'table _ name' with the key name: x value: 1.

 

Example of a Select query statement

The Code is as follows:

<? Php

$ Con = mysql_connect ("localhost", "peter", "abc123 ");

If (! $ Con)

{

Die ('could not connect: '. mysql_error ());

}

Mysql_select_db ("my_db", $ con );

$ Result = mysql_query ("SELECT * FROM Persons ");

While ($ row = mysql_fetch_array ($ result ))

{

Echo $ row ['firstname']. "". $ row ['lastname'];

Echo "<br/> ";

}

Mysql_close ($ con );

?>

 

 

Example

 

The Code is as follows:

<? Php

Define ('hostname', 'localhost'); // specifies the Database Host Name.

Define ('username', 'username'); // database USERNAME

Define ('Password', 'Password'); // Database User Logon PASSWORD

Define ('database _ name', 'testdb'); // the DATABASE to be queried

$ Db = mysql_connect (HOSTNAME, USERNAME, PASSWORD) or

Die (mysql_error ());

// If the connection fails, the cause of the mysql error is displayed.

Mysql_select_db (DATABASE_NAME );

// Switch to testdb www.jb51.net

$ Query =

"SELECT uri, title FROM testdb WHERE 1 ORDER by rand () LIMIT 1 ";

// The above sentence means to randomly extract a piece of data from testdb.

$ Result = mysql_query ($ query );

// Query

While ($ row = mysql_fetch_array ($ result) {echo "<p id =" title "> ",

($ Row ['title']), "</p> <p id =" uri ">-", nl2br ($ row ['uri'])

, "</P> ";}

// Display the result

Mysql_free_result ($ result );

// Release result

Mysql_close ();

// Close the connection

?>

 

 

Garbled mysql Chinese Data

The database uses UTF8 encoding, while the page declarative encoding is GB2312, which is the most common cause of garbled code. In this case, the SELECT data in the PHP script is garbled and needs to be used before query:

The Code is as follows:

Mysql_query ("set names gbk"); or mysql_query ("set names GB2312 ");

 

 

To set the MYSQL connection encoding to ensure that the page declarative encoding is consistent with the connection encoding set here (GBK is an extension of GB2312 ). If the page is UTF-8 encoded, you can use:

Mysql_query ("set names UTF8"); note that UTF8 is not a commonly used UTF-8. If the encoding stated on the page is consistent with the internal encoding of the database, no connection encoding can be set.

The Code is as follows:

 

The Code is as follows:

$ Mysql_mylink = mysql_connect ($ mysql_host, $ mysql_user, $ mysql_pass );

Mysql_query ("set names 'gbk '");

 

 

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.