gpt4 book ai didi

php - 错误 mysqli_insert_id

转载 作者:行者123 更新时间:2023-11-29 21:34:05 24 4
gpt4 key购买 nike

使用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

但我不断收到此错误:

Warning: mysqli_insert_id () expects parameter exactly 1, 2 given in /home/controle/public_html/demo/teste.php on line 9

我该如何解决这个问题?

最佳答案

这是获取最后创建的记录的 id 的简单示例-

Code is taken from here

<?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);
?>

编辑:这是工作代码

<?php
define("SERVIDOR_BD", "localhost");
define("USUARIO_BD", "usuario");
define("SENHA_BD", "senha");
define("BANCO_DE_DADOS", "dados");


class conecta {
public $database_bancoDados = null;//1. New Added Field
public $bancoDados = null;//2. New Added Field
function conecta($servidor="", $bancoDeDados="", $usuario="", $senha=""){
if (($servidor == "") && ($usuario == "") && ($senha == "") && ($bancoDeDados == "")){
$this->bancoDados = mysqli_connect(SERVIDOR_BD, USUARIO_BD, SENHA_BD) or trigger_error(mysqli_error(),E_USER_ERROR);//3. Store in class variable
$this->database_bancoDados = BANCO_DE_DADOS;//4. Store in class variable
} else {
$this->bancoDados = mysqli_connect($servidor, $usuario, $senha) or trigger_error(mysqli_error(),E_USER_ERROR);//5. Store in class variable
$this->database_bancoDados = $bancoDeDados;//6. Store in class variable
}
}
}

class consultar {

var $bd;
var $res;
var $row;
var $nrw;
var $data;

function executa($sql=""){
if($sql==""){
$this->res = 0; // Pointer result of the executed query
$this->nrw = 0; // Line number the query returned, cruise control
$this->row = -1; // Array of the current query line
}
// Connects to the database
$this->bd = new conecta();//7. Store in class variable
$this->bd->conecta();//8. Store in class variable
mysqli_select_db($this->bd->bancoDados, BANCO_DE_DADOS);//9. Change Here For parameter sequence
$this->res = mysqli_query($this->bd->bancoDados, $sql); //10. Change here for parameter sequence

$this->nrw = @mysqli_num_rows($this->res);

$this->row = 0;
if($this->nrw > 0)
$this->dados();
}

function primeiro(){
$this->row = 0;
$this->dados();
}

function proximo(){
$this->row = ($this->row<($this->nrw - 1)) ?
++$this->row:($this->nrw - 1);
$this->dados();
}

function anterior(){
$this->row = ($this->row > 0) ? -- $this->row:0;
$this->dados();
}

function ultimo(){
$this->row = $this->nrw-1;
$this->dados();
}

function navega($linha){
if($linha>=0 AND $linha<$this->nrw){
$this->row = $linha;
$this->dados();
}
}

function dados(){
mysqli_data_seek($this->res, $this->row);
$this->data = mysqli_fetch_array($this->res);
}
}

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

echo "New record has id: " . mysqli_insert_id($listar->bd->bancoDados);//11. Change Here for parameter

关于php - 错误 mysqli_insert_id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35042592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com