【问题标题】:Return ID of column from table with mysql_insert_id使用 mysql_insert_id 从表中返回列的 ID
【发布时间】:2014-05-21 19:18:08
【问题描述】:
$sql="INSERT INTO survey (user, title, description, opta, optb,optc,optd,time) VALUES ('$user','$title', '$dis', '$a' , '$b', '$c', '$d','timespan')";
if (mysqli_query($con,$sql))
 {
  echo "Success";
 }
 else
  {
  echo "Error: " . mysql_error();
  }
  echo $id = mysql_insert_id();

mysqli_close($con); 

我想从表中返回列的 id,但它返回 0。为什么?还有我该如何解决这个问题?提前致谢

【问题讨论】:

  • 您可能忘记在'timespan' 中添加$ 吗? IE。 '$timespan' 加上约翰在回答中提到的内容。顺便说一句,这不是朗姆酒和可乐,就像“他们不混合”;-)
  • 在开发过程中将错误报告添加到文件顶部error_reporting(E_ALL); ini_set('display_errors', 1);

标签: php mysql mysqli mysql-insert-id


【解决方案1】:

您正在混合使用 mysqlmysqli 函数。变化:

echo $id = mysql_insert_id();

echo $id = mysqli_insert_id($con);

您还需要将mysql_error() 更改为mysqli_error()

不这样做,不会表明发现任何可能的错误。

【讨论】:

  • @Fred-ii- 哦,是的。那个也是。 ;)
  • 赢了!我告诉过你我们是一支伟大的团队;-)
猜你喜欢
  • 2016-12-13
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
  • 2013-04-13
  • 2013-03-21
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多