Example of using SELECT query statements in PHP _php tutorial

Source: Internet
Author: User
SQL has many statements about database operations. But what is common and what is needed is a few more statements. The SELECT statement is used to select data from the database.

So let's first introduce the first SELECT statement.
Statement 1:select * FROM table_name
Explanation: The meaning is to read the entire table table_name inside the data show out
Statement 1:select * FROM table_name Where x = 1
Explanation: The meaning is to read the table table_name the key name is: X value: 1 of the data displayed

example of a Select query statement
Copy CodeThe code is as follows:
$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 "
";
}
Mysql_close ($con);
?>

Example

Copy the Code code as follows:
Define (' HOSTNAME ', ' localhost '); Database host Name
Define (' USERNAME ', ' USERNAME '); Database user Name
Define (' PASSWORD ', ' PASSWORD '); Database User Login Password
Define (' database_name ', ' testdb '); Databases that need to be queried
$db = mysql_connect (HOSTNAME, USERNAME, PASSWORD) or
Die (Mysql_error ());
If you do not connect, the reason for 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 meaning of the above sentence is to extract a random data from the TestDB.
$result = mysql_query ($query);
Inquire
while ($row = Mysql_fetch_array ($result)) {echo "

" ,
($row [' title ']), "

– ", NL2BR ($row [' URI '])
, "

"; }
Show results
Mysql_free_result ($result);
Release results
Mysql_close ();
Close connection
?>

MySQL Chinese data garbled
Database using UTF8 encoding, and page declaration code is GB2312, which is the most common cause of garbled. At this time in the PHP script directly inside the Select data is garbled, need to be used before the query:
Copy the Code code as follows:
mysql_query ("Set NAMES GBK"), or mysql_query ("set NAMES GB2312");

To set the MySQL connection code to ensure that the page declaration code is consistent with the connection code 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 it is a UTF8 rather than a general UTF-8. If the code of the page declaration is consistent with the internal encoding of the database, the connection code can be set.
The code is as follows:
Copy the Code code as follows:
$mysql _mylink = mysql_connect ($mysql _host, $mysql _user, $mysql _pass);
mysql_query ("SET NAMES ' GBK '");

http://www.bkjia.com/PHPjc/825291.html www.bkjia.com true http://www.bkjia.com/PHPjc/825291.html techarticle SQL has many statements about database operations. But what is common and what is needed is a few more statements. The SELECT statement is used to select data from the database. So let's start by introducing the first kind of ...

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