Examples of using SELECT query statements in PHP

Source: Internet
Author: User
Tags php script mysql database

 php to query the contents of the MySQL database we must first connect the MySQL database, and then use the SQL statement to query, let's take a look at some examples.

SQL has many statements to manipulate the database. But the common and comparative needs are so few statements. The SELECT statement is used to select data from the database.     Then let's introduce the first SELECT statement statement 1:select *  FROM table_name Explanation: The meaning is to read the entire table table_name the data inside the statement 1:select * from TA Ble_name Where x = 1 Explanation: Meaning is to read the table table_name inside the key name: X value is: 1 of the data displayed     Select Query Statement example   code as follows: <?php $con = mysq L_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     code as follows: <?php  define (' HOSTNAME ', ' localhost ') ); Database host name   define (' USERNAME ', ' USERNAME '); Database username   define (' PASSWORD ', ' PASSWORD '); Database user Login password   define (' database_name ', ' testdb '); Database to query   $DB = mysql_connect (HOSTNAME, USERNAME, PASSWORD) or          die (Mysql_error ()); //connection is not on, it will show the reason for MySQL error.   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 means to randomly extract a piece of data from the TestDB.   $result = mysql_query ($query); //query   while ($row = Mysql_fetch_array ($result)) {echo <p id= "title" & gt; ",  ($row [' title '])," </p><p id= "uri" >– ", NL2BR ($row [' URI '])  ," </p> "; } //Display results   mysql_free_result ($result); //Release results   mysql_close (); //close connection  ?>     MySQL Chinese data garbled database using UTF8 code, and the page is GB2312 code, which is the most common cause of garbled. At this time in the PHP script directly select the data out is garbled, need to use before the query:   code as follows: mysql_query ("Set NAMES GBK"); 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"); Attention is UTF8 rather than 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: The   code is as follows: $mysql _mylink = mysql_connect ($mysql _host, $mysql _uSer, $mysql _pass); mysql_query ("SET NAMES ' GBK '");    
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.