Home > Backend Development > PHP Tutorial > Why must the if judgment be written??? Solve...

Why must the if judgment be written??? Solve...

WBOY
Release: 2016-09-14 09:41:20
Original
991 people have browsed it

(My front-end, php novice)

Excuse me, after getting the front-end data, why do you have to write it when inserting it into the database

<code>if ($conn -> query($sql) == true){echo"插入数据库成功"}</code>
Copy after login
Copy after login

In this sentence, inserting data can be successful. Isn’t this a judgment? If you don’t judge, just insert the data

<code>$sql = "insert into yihe (name, psw) values ('$name', '$psw')";</code>
Copy after login
Copy after login

Isn’t it possible to insert it directly into the database????

Reply content:

(My front-end, php novice)

Excuse me, after getting the front-end data, why do you have to write it when inserting it into the database

<code>if ($conn -> query($sql) == true){echo"插入数据库成功"}</code>
Copy after login
Copy after login

In this sentence, inserting data can be successful. Isn’t this a judgment? If you don’t judge, just insert the data

<code>$sql = "insert into yihe (name, psw) values ('$name', '$psw')";</code>
Copy after login
Copy after login

Isn’t it possible to insert it directly into the database????

Some database operations will have return values, such as insert, modify, etc. If the operation is successful, it will return true, if it fails, it will return false, and then operate based on the result of the return value;

The questioner said that the insertion failed without writing if, which means your understanding is wrong. You can assign the execution result of $conn->query($sql) to a value, such as the following:

<code>$flag = $conn->query($sql);
if($flag == TRUE){  //如果返回值为TRUE 说明插入成功 否则就插入失败
    echo 'nice!';
}else {
    echo 'error!';
} </code>
Copy after login

The questioner can take a look at some basic knowledge of databases, and the questions you ask will be easily answered.

SQL statements may fail to execute. For example, the database process suddenly crashes, or the memory explodes, or even high concurrency (the number of mysql queries that an ordinary notebook can execute per second is only a few hundred, and if there are too many, it cannot be executed correctly)

If you want to insert data directly, no problem, just write like this:

<code>$conn->query($sql);</code>
Copy after login

What ififor notif? Anyway, writing this way is OK most of the time and can run, but it is not standardized enough and has hidden dangers.

Because there are two results, one is success and the other is failure. Success has success processing (prompt that the processing is successful), and failure has failure processing (such as prompts for the user to modify the input)

Because writing is actually executed after running to $conn->query($sql)

http://php.net/mysqli

To confirm that inserting a piece of data is successful, two conditions need to be met:
First, there is no syntax error in the SQL statement, which is reflected in query() not returning false.
Second, the affected behavior 1 row, reflected in affected_rows equal to 1.
Other update/delete and other write operations are similar.

<code>if ($mysqli->connect_errno) //判断连接是否成功,错误信息$mysqli->connect_error
if ($mysqli->query())       //判断SQL语句执行是否成功,错误编号$mysqli->errno,错误信息$mysqli->error
if ($stmt->prepare())       //判断SQL语句预处理是否成功,错误编号$mysqli->errno,错误信息$mysqli->error</code>
Copy after login
<code>CREATE TABLE IF NOT EXISTS mem (
    k varchar(32) NOT NULL,
    v text NOT NULL,
    t bigint(20) unsigned NOT NULL,
    PRIMARY KEY (k) USING HASH,
    KEY (t) USING HASH
) ENGINE=MEMORY DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

$db = @new mysqli('127.0.0.1','root','pass','yabase',3306);
if ($db->connect_errno) {
    //如密码错误 1045: Access denied for user 'root'@'localhost' (using password: YES)
    echo $db->connect_errno.': '.$db->connect_error."\n";
    exit();
}
$db->query("INSERT INTO mem (k, v, t) VALUES ('key1', 'value1', '20160906173140')");
if ($db->errno) {
    //如主键重复 1062: Duplicate entry 'key1' for key 'PRIMARY'
    echo $db->errno.': '.$db->error."\n";
    exit();
}
if ($db->affected_rows == 1) {
    echo 'affected_rows: '.$db->affected_rows."\n";
}</code>
Copy after login
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