How to query database content in php: 1. Execute select query through "mysqli_query()" method; 2. Query through "PDO::__query()" method.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How to query the database content with php?
PHP MySQL Select query data
PHP mysql_query() function is used to execute select queries. Since PHP 5.5, the mysql_query() function is deprecated. Now, it is recommended to use one of the following 2 alternatives.
mysqli_query() PDO::__query()
There are two other MySQLi functions in the select query.
mysqli_num_rows(mysqli_result $result): Returns the number of rows. mysqli_fetch_assoc(mysqli_result $result): Returns an associative array of rows. Each key name of the array is a column name of the table. If there is no row data, NULL is returned.
PHP MySQLi select query example
<?php $host = 'localhost:3306'; $user = 'root';// $pass = ''; $dbname = 'test'; $conn = mysqli_connect($host, $user, $pass,$dbname); if(!$conn){ die('Could not connect: '.mysqli_connect_error()); } echo 'Connected successfully<br/>'; $sql = 'SELECT * FROM emp4'; $retval=mysqli_query($conn, $sql); if(mysqli_num_rows($retval) > 0){ while($row = mysqli_fetch_assoc($retval)){ echo "EMP ID :{$row['id']} <br> ". "EMP NAME : {$row['name']} <br> ". "EMP SALARY : {$row['salary']} <br> ". "--------------------------------<br>"; } //end of while }else{ echo "0 results"; } mysqli_close($conn); ?>
PHP
Execute the above code to get the following results-
Tip: There must be relevant data in the emp4 table
Connected successfully EMP ID :1 EMP NAME : maxsu EMP SALARY : 9000 -------------------------------- EMP ID :2 EMP NAME : minsu EMP SALARY : 40000 -------------------------------- EMP ID :3 EMP NAME : jaizhang EMP SALARY : 90000 --------------------------------
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to query database content in php. For more information, please follow other related articles on the PHP Chinese website!