search
HomeBackend DevelopmentPHP ProblemHow 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.

How to make a query statement using php

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 id="Finding-nbsp-users-nbsp-from-nbsp-mysql-nbsp-database">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 id="User-nbsp-found-nbsp-from-nbsp-mysql-nbsp-database">User found from mysql database.</h2>
<?php
    $gender = $_POST[&#39;gender&#39;];
    if(!$gender){
        echo "Error: There is no data passed.";
        exit;
    }
    if(!get_magic_quotes_gpc()){
        $gender = addslashes($gender);
    }
        @ $db = mysqli_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;12345678&#39;);
        mysqli_select_db($db,&#39;testphp&#39;);
        if(mysqli_connect_errno()){
         echo "Error: Could not connect to mysql database.";
         exit;
        }
        $q = "SELECT * FROM user WHERE gender = &#39;".$gender."&#39;";
        $result = mysqli_query($db,$q);
        $rownum = mysqli_num_rows($result);
        for($i=0; $i<$rownum; $i++){
           $row = mysqli_fetch_assoc($result);
           echo "Id:".$row[&#39;id&#39;]."<br />";
           echo "Name:".$row[&#39;name&#39;]."<br />";
           echo "Age:".$row[&#39;age&#39;]."<br />";
           echo "Gender:".$row[&#39;gender&#39;]."<br />";
           echo "Info:".$row[&#39;info&#39;]."<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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.