Home > Article > Backend Development > How to make a query statement using php
How to use PHP to make a query statement: 1. Create a PHP sample file; 2. Use mysqli_connect to connect to the database; 3. Pass "$q = "SELECT * FROM user WHERE gender = '".$gender ."'";$result = mysqli_query($db,$q);" statement can be used to implement the query.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
How to use php to make a query statement?
php uses select statements to query data information
<html> <head> <title>Finding User</title> </head> <body> <h2>Finding users from mysql database.</h2> <form action="selectformhandler.php" method="post"> Select gender: <select name="gender"> <option value="male">man</option> <option value="female">woman</option> </select><br /> <input name="submit" type="submit" value="Find"/> </form> </body> </html>
<html> <head> <title>User found</title> </head> <body> <h2>User found from mysql database.</h2> <?php $gender = $_POST['gender']; if(!$gender){ echo "Error: There is no data passed."; exit; } if(!get_magic_quotes_gpc()){ $gender = addslashes($gender); } @ $db = mysqli_connect('localhost','root','12345678'); mysqli_select_db($db,'testphp'); if(mysqli_connect_errno()){ echo "Error: Could not connect to mysql database."; exit; } $q = "SELECT * FROM user WHERE gender = '".$gender."'"; $result = mysqli_query($db,$q); $rownum = mysqli_num_rows($result); for($i=0; $i<$rownum; $i++){ $row = mysqli_fetch_assoc($result); echo "Id:".$row['id']."<br />"; echo "Name:".$row['name']."<br />"; echo "Age:".$row['age']."<br />"; echo "Gender:".$row['gender']."<br />"; echo "Info:".$row['info']."<br />"; } mysqli_free_result($result); mysqli_close($db); ?> </body> </html>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to make a query statement using php. For more information, please follow other related articles on the PHP Chinese website!