gpt4 book ai didi

php - 主键错误(自动增量int)在php中调用存储过程

转载 作者:行者123 更新时间:2023-11-29 16:13:09 26 4
gpt4 key购买 nike

//存储过程名称:insertName,_id为自增

try { 

$sql = "CALL insertName(:_id, :_name)";
$res = $pdo->prepare($sql);
$res -> bindValue(":_id","NULL");
$res -> bindValue(":_name",$name);
$res->execute();
$res->setFetchMode(PDO::FETCH_ASSOC);
} catch( PDOException $e ) {

die( "ERROR ..." .$e->getMessage());
}

//如果我尝试直接插入数字,一切都会正常

try {  
$sql = "CALL insertName(:_id, :_name)";

$res = $pdo->prepare($sql);
$res -> bindValue(":_id",1);
$res -> bindValue(":_name",$name);
$res->execute();
$res->setFetchMode(PDO::FETCH_ASSOC);
} catch( PDOException $e ) {

die( "ERROR ..." .$e->getMessage());
}

//即使我在不​​使用存储过程的情况下尝试此操作也可以正常

try {  
$sql = 'INSERT INTO (id,name) VALUES (NULL,"'.$name.'" )';
$res = $pdo->prepare($sql);
$res->execute();
} catch( PDOException $e ) {

die( "ERROR ..." .$e->getMessage());
}

最佳答案

只插入name值,由于id是自增的,所以会自己取值

关于php - 主键错误(自动增量int)在php中调用存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55076272/

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