执行一次查询的 PHP MySQL INSERT 返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1488237/
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
PHP MySQL INSERT return value with one query execution
提问by Phill Pafford
Is there anything returned from MySQL/PHP on a INSERT query being executed? Here is my function which I have in a CLASS.
在执行 INSERT 查询时是否有任何从 MySQL/PHP 返回的内容?这是我在 CLASS 中的函数。
function mysqlQuery($query) {
// Gets the results from the query
$results = mysql_query($query, $this->connection);
// Loops through the queried results as an multi-dimensional array
while($rows = mysql_fetch_array($results, MYSQL_ASSOC)) {
// Push results to single array
$rs[] = $rows;
}
// Return results as array
return $rs;
}
This is how I call the function
这就是我调用函数的方式
$rs = $dbh->mysqlQuery($query);
But executing a INSERTquery the $rsreturns nothing. Does my function need help or is this the default behavior? Any tips would be helpful as well.
但是执行INSERT查询$rs不会返回任何内容。我的函数需要帮助还是这是默认行为?任何提示也会有所帮助。
回答by helloandre
INSERT just returns true or false. to actually return something useful, you will need a SELECT or similar query. there is no result to fetch with INSERT.
INSERT 只返回真或假。要真正返回有用的东西,您将需要一个 SELECT 或类似的查询。没有结果可以用 INSERT 获取。
回答by Ricardo
From the php documentation:
从 php 文档:
Return Values
For SELECT, SHOW, DESCRIBE, EXPLAINand other statements returning resultset, mysql_query()returns a resource on success, or FALSEon error.
返回值SELECT,SHOW,DESCRIBE,EXPLAIN等语句返回的结果集,mysql_query()成功返回的资源,或者FALSE在错误。
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query()returns TRUEon success or FALSEon error.
对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP,等,mysql_query()收益TRUE上的成功或FALSE出错。
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
返回的结果资源应传递给mysql_fetch_array(),以及其他处理结果表的函数,以访问返回的数据。
Use mysql_num_rows()to find out how many rows were returned for a SELECT statementor mysql_affected_rows()to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.
使用mysql_num_rows()找出多少行以被退回SELECT statement或mysql_affected_rows()找出多少行受一DELETE,INSERT,REPLACE,或者UPDATE statement.
mysql_query()will also fail and return FALSEif the user does not have permission to access the table(s) referenced by the query.
mysql_query()FALSE如果用户没有访问查询引用的表的权限,也将失败并返回。
回答by Chiwda
Although this is a very old thread, it is still relevant. And to those who stumble upon this (like I did), don't give up! There are options. In fact, see this answer on SO
尽管这是一个非常古老的线程,但它仍然具有相关性。对于那些偶然发现这一点的人(就像我一样),不要放弃!有选择。事实上,在SO上看到这个答案
Also, for sqli and pdo, see this
另外,对于 sqli 和 pdo,请参阅此
Essentially, an insert statement followed by one of the functions listed in those answers will give you the ID of the last record. The function is used like this:
本质上,插入语句后跟这些答案中列出的函数之一将为您提供最后一条记录的 ID。该函数是这样使用的:
$LastID = mysql_insert_id();
after the INSERT statement.
在 INSERT 语句之后。
回答by Chiwda
From php.net http://us2.php.net/mysql_query
来自 php.net http://us2.php.net/mysql_query
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.
对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 在成功时返回资源,在错误时返回 FALSE。
对于其他类型的 SQL 语句,INSERT、UPDATE、DELETE、DROP 等,mysql_query() 在成功时返回 TRUE,在错误时返回 FALSE。
回答by swordofpain
Generally, when devs are building home functions to manipulates queries, they use two methods. One, "query", used for SELECTs and co, and one, "exec", other INSERTs, UPDATEs and others (like in PDO extension).
通常,当开发人员构建 home 函数来操作查询时,他们使用两种方法。一个,“查询”,用于 SELECT 和 co,一个,“exec”,其他 INSERT,UPDATE 和其他(如在 PDO 扩展中)。
If you want to keep your function, add a IF statement around the loop checking the type of $results (with a is_resource() for example)
如果你想保留你的函数,在循环周围添加一个 IF 语句来检查 $results 的类型(例如使用is_resource() )
回答by shamsieh76
In the example below, I add to my insert clause the "returning" along with the primary key of my table, then after the execute, I do a fetch getting an array with the value of the last inserted id.
在下面的示例中,我将“返回”与表的主键一起添加到我的插入子句中,然后在执行后,我执行获取具有最后插入的 id 值的数组。
<?php
public function insert($employee){
$sqlQuery = "INSERT INTO employee(user_id,name,address,city) VALUES(:user_id,:name,:address,:city) RETURNING employee_id";
$statement = $this->prepare($sqlQuery);
$a ="2002-03-11 12:01AM" ;
$statement->bindParam(":user_id", $employee->getUserId(), PDO::PARAM_INT);
$statement->bindParam(":name", $employee->getName(), PDO::PARAM_STR);
$statement->bindParam(":address", $employee->getAddress(), PDO::PARAM_STR);
$statement->bindParam(":city", $employee->getCity(), PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetch(PDO::FETCH_ASSOC);
return $result["employee_id"];
}
?>

