MySQL databases use SQL SELECT statements to query data.
You can query data in the database through the mysql> command prompt window, or through a PHP script.
The following is the general SELECT syntax for querying data in a MySQL database:
SELECT column_name,column_name FROM table_name [WHERE Clause][OFFSET M ][LIMIT N]
- You can use one or more tables in the query statement, separate tables with commas (,), and use the WHERE statement to set query conditions.
- The SELECT command can read one or more records.
- You can use an asterisk (*) in place of other fields, and the SELECT statement will return all field data in the table.
- You can use the WHERE statement to include any condition.
- You can specify the data offset at which the SELECT statement starts the query with OFFSET. By default the offset is 0.
- You can use the LIMIT property to set the number of records returned.
Get data via command prompt
The following example will return all records of the data table runoob_tbl:
[root@localhost runoob]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.50-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use RUNOOB
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [RUNOOB]> select * from runoob_tbl
-> ;
+-----------+---------------+---------------+-----------------+
| runoob_id | runoob_title | runoob_author | submission_date |
+-----------+---------------+---------------+-----------------+
| 1 | Learn PHP | John Poul | 2016-11-26 |
| 2 | Learn MySQL | Abdul S | 2016-11-26 |
| 3 | JAVA Tutorial | Sanjay | 2007-05-06 |
| 4 | mysql | cakin24 | 2016-11-26 |
+-----------+---------------+---------------+-----------------+
4 rows in set (0.00 sec)
MariaDB [RUNOOB]>
使用PHP脚本来获取数据
方法一:
首先,使用PHP函数mysql_query()及SQL SELECT命令来获取数据,mysql_query函数用于执行SQL命令。
然后,通过PHP函数mysql_fetch_array() 来处理查询出的数据。
mysql_fetch_array() 函数从结果集中取得一行作为关联数组,或数字数组,或二者兼有。 返回从结果集取得的行生成的数组,如果没有查询结果,则返回 false。
以下实例为从数据表 runoob_tbl 中读取所有记录。
尝试以下实例来显示数据表 runoob_tbl 的所有记录。
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT runoob_id, runoob_title,
runoob_author, submission_date
FROM runoob_tbl';
mysql_select_db('RUNOOB');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "Tutorial ID :{$row['runoob_id']} <br> ".
"Title: {$row['runoob_title']} <br> ".
"Author: {$row['runoob_author']} <br> ".
"Submission Date : {$row['submission_date']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
以上实例中,读取的每行记录赋值给变量$row,然后再打印出每个值。
注意:记住如果你需要在字符串中使用变量,请将变量置于花括号。
在上面的例子中,PHP mysql_fetch_array()函数第二个参数为MYSQL_ASSOC, 当设置为该参数,查询结果会返回关联数组,你可以使用字段名称来作为数组的索引。
方法二:
PHP提供了另外一个函数mysql_fetch_assoc(), 该函数从结果集中取得一行作为关联数组。 返回根据从结果集取得的行生成的关联数组,如果没有更多行,则返回 false。
下面实例使用了mysql_fetch_assoc()函数来输出数据表runoob_tbl的所有记录:
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT runoob_id, runoob_title,
runoob_author, submission_date
FROM runoob_tbl';
mysql_select_db('RUNOOB');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_assoc($retval))
{
echo "Tutorial ID :{$row['runoob_id']} <br> ".
"Title: {$row['runoob_title']} <br> ".
"Author: {$row['runoob_author']} <br> ".
"Submission Date : {$row['submission_date']} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
方法三:
也可以使用常量 MYSQL_NUM 作为PHP mysql_fetch_array()函数的第二个参数,返回数字数组。
以下实例使用MYSQL_NUM参数显示数据表runoob_tbl的所有记录:
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT runoob_id, runoob_title,
runoob_author, submission_date
FROM runoob_tbl';
mysql_select_db('RUNOOB');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_NUM))
{
echo "Tutorial ID :{$row[0]} <br> ".
"Title: {$row[1]} <br> ".
"Author: {$row[2]} <br> ".
"Submission Date : {$row[3]} <br> ".
"--------------------------------<br>";
}
echo "Fetched data successfully\n";
mysql_close($conn);
?>
以上三个实例输出结果都一样,输出结果如下:
内存释放
在我们执行完SELECT语句后,释放游标内存是一个很好的习惯 。可以通过PHP函数mysql_free_result()来实现内存的释放。
以下实例演示了该函数的使用方法。该实例仅在上一个实例的基础上加了mysql_free_result($retval);这一语句。
<?php
$dbhost = 'localhost:3036';
$dbuser = 'root';
$dbpass = 'rootpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}
$sql = 'SELECT runoob_id, runoob_title,
runoob_author, submission_date
FROM runoob_tbl';
mysql_select_db('RUNOOB');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_NUM))
{
echo "Tutorial ID :{$row[0]} <br> ".
"Title: {$row[1]} <br> ".
"Author: {$row[2]} <br> ".
"Submission Date : {$row[3]} <br> ".
"--------------------------------<br>";
}
mysql_free_result($retval);
echo "Fetched data successfully\n";
mysql_close($conn);
?>