Home headlines Database query operation in php

Database query operation in php

Jun 28, 2018 am 11:35 AM

Database query operation in php

Recommended related mysql video tutorials: "mysql tutorial"

* 1. Query Operation

* 2. Three functions involved:

* 2-1. mysqli_query($db, $sql): Execute SQL statement and return the result set

* 2-2. mysqli_errno($db): Returns the error code of the last function execution

* 2-3. mysqli_error($db): Returns the error message of the last function execution

* 2-4. mysqli_num_rows($res): Returns the number of records in the result set

* 2-5. mysqli_fetch_row($res): Returns a record in an index array

* 2-5. mysqli_fetch_assoc($res): Returns a record as an associative array

* 2-5. mysqli_fetch_array($res,MYSQLI_NUM): Returns a record as an index array

* 2-5. mysqli_fetch_array( $res,MYSQLI_ASSOC): Returns a record as an associative array

* 2-5. mysqli_fetch_array($res,MYSQLI_BOTH): Returns a record as an index and relational array

* 2-5 . mysqli_fetch_object($res): Return a record in object mode

* mysqli_free_result($result);

* 2-4. mysqli_close($db): Close the current data connection

* 3. Query steps:

* 3-1. Connect to the database

* 3-2. Prepare SQL statements

* 3-3. Execute query

* 3-4. Detection result

* 3-5. If it is select, return the result set

* 3-5-1. If it is insert/update /delete: Returns the number of affected records

* 3-6. Close the database connection

//1. Connect to the database, require is not a function, no need to add brackets after it

define ('DB_HOST', 'localhost');
define ('DB_USER', 'root');
define ('DB_PASS', 'root');
define ('DB_NAME', 'php');
define ('DB_CHAR', 'utf8');
$db = @mysqli_connect(DB_HOST, DB_USER, DB_PASS);
if (mysqli_connect_errno($db)) {
    exit('连接失败'.mysqli_connect_error($db));
}
mysqli_select_db($db, DB_NAME);
mysqli_set_charset($db, DB_CHAR);
Copy after login

//2. Prepare SQL statement

$sql = "SELECT name,salary FROM staff";
$sql = "SELECT name AS 姓名,salary AS 工资 FROM staff WHERE age>90";
Copy after login

//3. Execute the query: the result set object will be returned if successful, false

$result = mysqli_query($db, $sql);
var_dump($result);die();
Copy after login

//4. Detection results

//If the result set exists

if (false != $result) { //这是写只是语义性更强
    //如果结果集中存在记录,至少有一条
//    if (mysqli_num_rows($result) > 0) {
    //mysqli_affected_rows($db)也可以完成同样的检测工作,注意参数是连接对象$db,不是结果集对象
    if (mysqli_affected_rows($db) > 0) {
//        echo &#39;共计:&#39;.mysqli_num_rows($result).&#39;条记录<br>&#39;;
        echo &#39;共计:&#39;.mysqli_affected_rows($db).&#39;条记录~~<br>&#39;;
Copy after login

//5. Process the result set

//5-1: Get the index part

while($row = mysqli_fetch_array($result, MYSQLI_NUM)){
Copy after login

//Get the index part Shortcut method

while($row = mysqli_fetch_row($result)){
Copy after login

//5-2: Get the associated part

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
Copy after login

//Shortcut method to get the associated part

while($row = mysqli_fetch_assoc($result)){
Copy after login

//5-3: Index association

while($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
Copy after login

//5-4: The quick way to get the index association is not to pass in the second parameter, which is the default value

while($row = mysqli_fetch_array($result)){
Copy after login

//5-4: In the form of objects

        while($row = mysqli_fetch_object($result)){
//            var_export($row);//以字符串方式表示查询结查
            //如果是对象,可以用指向符来访问
            echo $row->name.&#39;--&#39;.$row->salary;
            echo &#39;<hr>&#39;;
            //以变量方式表示,表示的内容更加完整丰富
//            var_dump($row);
        }
    } else {
        echo &#39;没有符合条件的记录&#39;;
    }
    //查询失败的处理方式
} else {
    //必须要用exit()或die()终止脚本执行,否则后面的语句还会执行并会报错
    exit(&#39;查询失败&#39;.mysqli_errno($db).&#39;:&#39;.mysqli_error($db));
}
Copy after login

//5. Release the result set (only for select)

mysqli_free_result($result);
Copy after login

//6. Close the database connection

mysqli_close($db);
Copy after login

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

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1252
24