Why if judgment must write??? Solving...

Source: Internet
Author: User
(I front, PHP small white one)

I ask you, you get to the front end of the data, insert it into the database this time, why do not write

if ($conn -> query($sql) == true){echo"插入数据库成功"}

This sentence, insert data to succeed, this is not a sentence judgment? If you do not judge the data directly

$sql = "insert into yihe (name, psw) values ('$name', '$psw')";

Insert database directly can't you????

Reply content:

(I front, PHP small white one)

I ask you, you get to the front end of the data, insert it into the database this time, why do not write

if ($conn -> query($sql) == true){echo"插入数据库成功"}

This sentence, insert data to succeed, this is not a sentence judgment? If you do not judge the data directly

$sql = "insert into yihe (name, psw) values ('$name', '$psw')";

Insert database directly can't you????

Some database operations will have a return value, for example, 插入 and 修改 so on, if the operation succeeds in returning true, the failure will return false, and then based on the results of the return value of the operation;

The main word does not write the insertion is unsuccessful if , stating that your understanding is wrong, you can $conn->query($sql) assign the execution result to a value, than the following:

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

Master can take a look at some of the basic knowledge of the database, you ask these questions will be solved .

SQL statements may fail to execute. such as the database process suddenly collapsed, or memory explosion, or even high concurrency (normal notebook MySQL per second to execute the number of query can be hundreds of, too many can not be executed correctly)

You want to insert data directly, no problem, just write it like this:

$conn->query($sql);

Don't care if if about it? Anyway this write most of the time is OK, can run, but not enough norms, there are hidden dangers.

Because there are two kinds of results, a success, a failure, successful processing (prompt processing succeeds), failure processing (such as prompting the user to modify the input)

Because the run-to $conn->query($sql) will actually execute the write

Http://php.net/mysqli
To determine the success of inserting 1 data, you need to meet two conditions:
One is that there is no syntax error in the SQL statement, which is reflected as query () does not return false.
The second is the affected behavior 1 lines, reflected as affected_rows equals 1.
Other write operations such as update/delete are similar.

if ($mysqli->connect_errno) //判断连接是否成功,错误信息$mysqli->connect_errorif ($mysqli->query())       //判断SQL语句执行是否成功,错误编号$mysqli->errno,错误信息$mysqli->errorif ($stmt->prepare())       //判断SQL语句预处理是否成功,错误编号$mysqli->errno,错误信息$mysqli->error
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";}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.