如何使用 PHP/MYSQL 在单个查询中执行 SELECT 和 INSERT?

发布于 2024-07-26 18:19:02 字数 876 浏览 5 评论 0原文

我有一个表 user_name ,其中包含 3 个字段:id、Name、Emailidauto_increment 字段)。 我想在 PHP 中执行以下查询,但它没有返回任何结果。

INSERT INTO user_name (Name, Email) VALUES ('Example', '[email protected]'); 
SELECT LAST_INSERT_ID() AS 'userid';

当我在 PHP 中执行上述查询时,如下所示,它不会返回任何内容。

$_SQL="INSERT INTO user_name (Name,Email) VALUES ('Example', '[email protected]'); 
SELECT LAST_INSERT_ID() AS 'userid';";

$result_last_id = @mysql_query($_SQL);
$rs_insert = mysql_fetch_array($result_last_id);

$new_userid = $rs_insert['userid'];

谁能告诉我如何将两个查询合并为一个。

I have a table user_name with 3 fields, id, Name, Email (id is auto_increment field). I want to execute the following query in PHP, but its not returning any result.

INSERT INTO user_name (Name, Email) VALUES ('Example', '[email protected]'); 
SELECT LAST_INSERT_ID() AS 'userid';

When I am executing the above query in PHP as below then its not returning anything.

$_SQL="INSERT INTO user_name (Name,Email) VALUES ('Example', '[email protected]'); 
SELECT LAST_INSERT_ID() AS 'userid';";

$result_last_id = @mysql_query($_SQL);
$rs_insert = mysql_fetch_array($result_last_id);

$new_userid = $rs_insert['userid'];

Can anyone please tell me how to execute both queries into one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

羁绊已千年 2024-08-02 18:19:02

看看 mysql_insert_id()< /a> 函数。

mysql_query($insertStatementOnly);
$new_userid = mysql_insert_id();

Give a look to the mysql_insert_id() function.

mysql_query($insertStatementOnly);
$new_userid = mysql_insert_id();
很快妥协 2024-08-02 18:19:02

看来您不需要执行多个查询,但我在下面介绍了如何执行此操作。 你想要的是最后插入的id,你可以从 <代码>mysql_insert_id

执行多个查询

来自 mysql_query

文档声称“不支持多个查询”。

但是,似乎支持多个查询。 您只需将标志 65536 作为 mysql_connect 的 5 参数(client_flags)传递。 该值在/usr/include/mysql/mysql_com.h中定义:
#define CLIENT_MULTI_STATMENTS (1UL << 16) /* 启用/禁用多 stmt 支持 */

同时执行多个查询时,mysql_query 函数将仅返回第一个查询的结果。 其他查询也将被执行,但您不会得到它们的结果。

或者,查看 mysqli 库,它有一个 multi_query 方法

It appears you don't need to execute multiple queries, but I included how to do it below. What you want is the last inserted id, which you get from mysql_insert_id.

To execute multiple queries

From comments on documentation of mysql_query:

The documentation claims that "multiple queries are not supported".

However, multiple queries seem to be supported. You just have to pass flag 65536 as mysql_connect's 5 parameter (client_flags). This value is defined in /usr/include/mysql/mysql_com.h:
#define CLIENT_MULTI_STATEMENTS (1UL << 16) /* Enable/disable multi-stmt support */

Executed with multiple queries at once, the mysql_query function will return a result only for the first query. The other queries will be executed as well, but you won't have a result for them.

Alternatively, have a look at the mysqli library, which has a multi_query method.

鱼窥荷 2024-08-02 18:19:02

答案很简单:你就是做不到。

http://php.net/mysql_query

Simple answer really: You just can't do it.

http://php.net/mysql_query

心碎无痕… 2024-08-02 18:19:02

我还建议您避免在 mysql_query 中使用错误抑制运算符“@”,因为您可能不会意识到任何 mysql 错误。 至少要做到

mysql_query($sql) or die("error: " . mysql_error()) ;

May I also suggest you avoid the error-suppression operator '@' in mysql_query as you may not be made aware of any mysql errors. At the very least do

mysql_query($sql) or die("error: " . mysql_error()) ;
苦笑流年记忆 2024-08-02 18:19:02

如果您将 Zend Framework 与 PDO 定义的 MySQL 数据库一起使用,您只需使用:

$database=Zend_Db::factory('PDO_MySQL',Array('hostname'=>'localhost','username'=>'x','password'=>'y','dbname'=>'z');
$connectionHandle=$database->getConnection();
$rowsInserted=$connectionHandle->insert('database_name','INSERT INTO x (a,b) VALUES (c,d)');
if ($rowsInserted>0) {
 $autoIncrementValue=$connectionHandle->lastInsertId();
}

If you are using the Zend Framework with a PDO defined MySQL database, you would just use:

$database=Zend_Db::factory('PDO_MySQL',Array('hostname'=>'localhost','username'=>'x','password'=>'y','dbname'=>'z');
$connectionHandle=$database->getConnection();
$rowsInserted=$connectionHandle->insert('database_name','INSERT INTO x (a,b) VALUES (c,d)');
if ($rowsInserted>0) {
 $autoIncrementValue=$connectionHandle->lastInsertId();
}
荭秂 2024-08-02 18:19:02

是的,您可以使用 Shell 命令

mysql-用户-p-h数据库-e'
SQL语句1;
SQL语句2;
SQL语句3;
……;
'

Php / MYSQL 程序员

Yes You can using Shell command <BR>
mysql -user -p -h database -e '
SQL STATEMENT 1;
SQL STATEMENT 2;
SQL STATEMENT 3;
.......;
'

Php / MYSQL Programmer

想挽留 2024-08-02 18:19:02

这可能会做你想要的:

insert into table1 (Name,Emails) values ('qqq','www');
select max(id) as lastinserted from table1;

this might do what u want:

insert into table1 (Name,Emails) values ('qqq','www');
select max(id) as lastinserted from table1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文