How to query database content in php

藏色散人
Release: 2023-03-10 22:32:01
Original
7037 people have browsed it

How to query database content in php: 1. Execute select query through "mysqli_query()" method; 2. Query through "PDO::__query()" method.

How to query database content in php

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()
Copy after login

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 = &#39;localhost:3306&#39;;  
$user = &#39;root&#39;;// 
$pass = &#39;&#39;;  
$dbname = &#39;test&#39;;  
$conn = mysqli_connect($host, $user, $pass,$dbname);  
if(!$conn){  
  die(&#39;Could not connect: &#39;.mysqli_connect_error());  
}  
echo &#39;Connected successfully<br/>&#39;;  
$sql = &#39;SELECT * FROM emp4&#39;;  
$retval=mysqli_query($conn, $sql);  
if(mysqli_num_rows($retval) > 0){  
 while($row = mysqli_fetch_assoc($retval)){  
    echo "EMP ID :{$row[&#39;id&#39;]}  <br> ".  
         "EMP NAME : {$row[&#39;name&#39;]} <br> ".  
         "EMP SALARY : {$row[&#39;salary&#39;]} <br> ".  
         "--------------------------------<br>";  
 } //end of while  
}else{  
echo "0 results";  
}  
mysqli_close($conn);  
?>
Copy after login

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 
--------------------------------
Copy after login

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!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!