如何使用 PHP/MYSQL 在单个查询中执行 SELECT 和 INSERT?
我有一个表 user_name
,其中包含 3 个字段:id、Name、Email
(id
是 auto_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
看看
mysql_insert_id()
< /a> 函数。Give a look to the
mysql_insert_id()
function.看来您不需要执行多个查询,但我在下面介绍了如何执行此操作。 你想要的是最后插入的id,你可以从 <代码>mysql_insert_id。
执行多个查询
来自
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
:Alternatively, have a look at the
mysqli
library, which has amulti_query
method.答案很简单:你就是做不到。
http://php.net/mysql_query
Simple answer really: You just can't do it.
http://php.net/mysql_query
我还建议您避免在 mysql_query 中使用错误抑制运算符“@”,因为您可能不会意识到任何 mysql 错误。 至少要做到
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
如果您将 Zend Framework 与 PDO 定义的 MySQL 数据库一起使用,您只需使用:
If you are using the Zend Framework with a PDO defined MySQL database, you would just use:
是的,您可以使用 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
这可能会做你想要的:
this might do what u want: