mysqli_insert_id错误

时间:2016-01-27 16:19:14

标签: php mysql mysqli

使用the PHP manual我创建了以下代码:

$query = "INSERT INTO inserir(nome) VALUES ('Stefanato');";
$listar = new consultar();
$listar->executa($query);

echo "New record has id: " . mysqli_insert_id($listar->$query);

我也将这个答案用于班级联系:Error mysqli_select_db

但我一直收到这个错误:

  

警告:mysqli_insert_id()期望参数正好为1,2   第9行/home/controle/public_html/demo/teste.php

我该如何解决?

1 个答案:

答案 0 :(得分:0)

以下是获取最后一条记录的ID的简单示例 -

Code is taken from here

function factorial(x) {
  if (x < 0) throw Error("Cannot calculate factorial of a negative number");
  return (function(f) {
    return f(f, x, 1);
  })(function(f, i, fact) {
    return i === 0 ? fact : f(f, i-1, i*fact);
  });
}

编辑:这是工作代码

<?php
    $con=mysqli_connect("localhost","my_user","my_password","my_db");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    mysqli_query($con,"INSERT INTO Persons (FirstName,LastName,Age)
        VALUES ('Glenn','Quagmire',33)");

    // Print auto-generated id
    echo "New record has id: " . mysqli_insert_id($con);

    mysqli_close($con);
?>