php 向MySql DB插入数据并显示插入成功或失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11650160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert data to MySql DB and display if insertion is success or failure
提问by sharon Hwk
I am inserting data to a MySQL DB, In case if the insertion fails i need to give an appropriate error message indicating so. According to my code below i will be Echo-ingthe message Insertedif the insertion was success or failure.
我正在将数据插入 MySQL 数据库,如果插入失败,我需要给出适当的错误消息来指示。根据我下面的代码Echo-ing,Inserted如果插入成功或失败,我将显示消息。
What i want to do is to echo the Insertedmessage only when the data insertion is a success, and return failif it fails. How can i modify my code to do so ?
我想要做的是Inserted仅当数据插入成功时才回显消息,fail如果失败则返回。我怎样才能修改我的代码呢?
<?php
mysql_connect("localhost", "root", "123") or die(mysql_error());
mysql_select_db("swm_database") or die(mysql_error());
mysql_query("INSERT INTO PEOPLE (NAME ) VALUES ('COLE')")or die(mysql_error());
echo "Inserted";
?>
回答by Kode Plus
$result = mysql_query("INSERT INTO PEOPLE (NAME ) VALUES ('COLE')"));
if($result)
{
echo "Success";
}
else
{
echo "Error";
}
回答by Timeless
According to the book PHP and MySQL for Dynamic Web Sites (4th edition)
根据书籍PHP 和 MySQL for Dynamic Web Sites (4th edition)
Example:
例子:
$r = mysqli_query($dbc, $q);
For simple queries like INSERT, UPDATE, DELETE, etc. (which do not return records), the $rvariable—short for result—will be either TRUEor FALSE, depending upon whether the query executed successfully.
对于INSERT、UPDATE、DELETE等简单查询(不返回记录),$r变量(结果的缩写)将为TRUE或FALSE,具体取决于查询是否成功执行。
Keep in mind that “executed successfully” means that it ran without error; it doesn't mean that the query's execution necessarily had the desired result; you'll need to test for that.
请记住,“成功执行”意味着它运行没有错误;这并不意味着查询的执行一定会有想要的结果;你需要为此进行测试。
Then how to test?
那怎么测试呢?
While the mysqli_num_rows()function will return the number of rows generated by a SELECTquery, mysqli_affected_rows()returns the number of rows affected by an INSERT, UPDATE, or DELETEquery. It's used like so:
虽然该mysqli_num_rows()函数将返回SELECT查询生成的行数,但mysqli_affected_rows()返回受INSERT、UPDATE或DELETE查询影响的行数。它是这样使用的:
$num = mysqli_affected_rows($dbc);
Unlike mysqli_num_rows(), the one argument the function takes is the database connection ($dbc), not the results of the previous query ($r).
与 不同mysqli_num_rows(),该函数采用的一个参数是数据库连接 ( $dbc),而不是前一个查询的结果 ( $r)。
回答by Omesh
After INSERTquery you can use ROW_COUNT()to check for successful insert operation as:
经过INSERT查询,你可以使用ROW_COUNT()检查成功的插入操作为:
SELECT IF(ROW_COUNT() = 1, "Insert Success", "Insert Failed") As status;
回答by mlishn
if (mysql_query("INSERT INTO PEOPLE (NAME ) VALUES ('COLE')")or die(mysql_error())) {
echo 'Success';
} else {
echo 'Fail';
}
Although since you have or die(mysql_error())it will show the mysql_error() on the screen when it fails. You should probably remove that if it isnt the desired result
虽然因为你有or die(mysql_error())它会在它失败时在屏幕上显示 mysql_error() 。如果它不是想要的结果,您可能应该删除它

