Home > Backend Development > PHP Tutorial > Examples of using Select query statements in Php_PHP tutorial

Examples of using Select query statements in Php_PHP tutorial

WBOY
Release: 2016-07-13 10:24:54
Original
1166 people have browsed it

SQL has many statements for database operations. But these are the few statements that are common and needed. The SELECT statement is used to select data from the database.

Then let’s first introduce the first SELECT statement
Statement 1: SELECT * FROM table_name
Explanation: It means reading the data in the entire table table_name and displaying it
Statement 1: SELECT * FROM table_name Where x = 1
Explanation: It means to read the data with the key name: 🎜>

Copy code
The 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 code

The code is as follows:

define ('HOSTNAME', 'localhost'); //Database host namedefine ('USERNAME', 'username'); //Database user name
define ('PASSWORD', 'password'); //Database user login password
define (' DATABASE_NAME', 'testdb'); //The database that needs to be queried
$db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or
die (mysql_error());
//If the connection cannot be made, it will Display the cause of 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 sentence means to randomly extract a piece of data from testdb.
$result = mysql_query($query);
//Query
while ($row = mysql_fetch_array($result)) { echo "

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

–" , nl2br($row['uri'])
, "

" ; }
//Display the result
mysql_free_result($result);
//Release the result
mysql_close();
//Close the connection
?>




MySQL Chinese data appears garbled
The database uses UTF8 encoding, and the page declaration encoding is GB2312. This is the most common reason for garbled characters. At this time, the direct SELECT data in the PHP script will be garbled. You need to use it before querying:


Copy the code

The code is as follows:

mysql_query("SET NAMES GBK"); or mysql_query("SET NAMES GB2312");
To set the MYSQL connection encoding, ensure that the page declaration 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 it is UTF8 instead of the commonly used UTF-8. If the encoding of the page declaration is consistent with the internal encoding of the database, you do not need to set the connection encoding.
The code is as follows:


Copy code

The code is as follows:

$mysql_mylink = mysql_connect($mysql_host, $mysql_user, $ mysql_pass);mysql_query("SET NAMES 'GBK'");



http://www.bkjia.com/PHPjc/825291.htmlwww.bkjia.com

truehttp: //www.bkjia.com/PHPjc/825291.htmlTechArticlesql has many statements for database operations. But these are the few statements that are common and needed. The SELECT statement is used to select data from the database. So let’s introduce the first one first...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template