【问题标题】:Conversion from MySQL to mysqli not working从 MySQL 到 mysqli 的转换不起作用
【发布时间】:2014-08-18 02:35:33
【问题描述】:

我正在将现有的 MySQL 转换为 mysqli

但我无法正确获取这段代码

mysql_query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");
$new_id = mysql_insert_id();
mysql_query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");

下面给出了我尝试的方法,但没有奏效

$mysqli->query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");
$new_id = mysqli_insert_id();
$mysqli->query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");

问题在于$new_id = mysqli_insert_id(); 语句,因为第一个查询正在执行

【问题讨论】:

  • 既然您要升级到mysqli,您应该花时间熟悉一下参数绑定和准备好的语句。
  • 过程风格的mysqli_*函数需要传递给它们的连接变量,这与mysql_*mysqli的面向对象版本不同
  • 你能给我一个关于如何从 mysql 转换为 mysqli 的详细教程的链接。我找不到一个好的
  • PHP manual 拥有您需要知道的一切
  • 这能回答你的问题吗? How to change mysql to mysqli?

标签: php mysql mysqli insert-id


【解决方案1】:

对于 mysqli 面向对象的样式来获取最后插入的 id 使用这个

$mysqli->insert_id ;

http://www.php.net/manual/en/mysqli.insert-id.php

所以你的查询将是

$mysqli->query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");

$new_id = $mysqli->insert_id;

$mysqli->query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");

【讨论】:

  • 所以我只需要在第二个查询的末尾插入它?它将如何插入到适当的列中。如果您不忙,请重写我提供的查询
  • 所以在第一个查询之后获取最后插入的 id 并在第二个查询中使用它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 2012-05-18
  • 2014-07-02
相关资源
最近更新 更多