繁体   English   中英

mysql_insert_id()不起作用

[英]mysql_insert_id() not working

<?php

$userid = $_SESSION['user_id'];
$price = $ad['price'];
$owner = $ad['owner']; //owner of advertisement

//make a bid for this advertisement
$query = "INSERT INTO bids (id, ad, bidder, bid, bidwhen, owner, quantity)
         VALUES (NULL, '$adid', '$userid','$price', now(), '$owner', 1)";
$bidData = mysqli_query($dbc, $query);

$message = $_POST['message']; //message to owner

if ($message != "") { //if message box is not empty insert comment
    $title = $ad['title'];
    $bidid = mysql_insert_id($bidData); //Line 123 get last id of bid insert and put it into message query for reference

    $query = "INSERT INTO messages (sentto, sentfrom, sentat, message, title, bid)
    VALUES ('$owner', '$userid', now(), '$message', '$title', '$bidid')";
    $messageData = mysqli_query($dbc, $query);
}

?>

错误信息:

mysql_insert_id() expects parameter 1 to be resource, boolean given

当我不传递参数时,出现此错误消息:

Warning: mysql_insert_id() [function.mysql-insert-id]: Access denied for user 'ODBC'@'localhost' (using password: NO) in C:\Users\Jonny\Desktop\projects\xampp\htdocs\phpprojects\lets\ad.php on line 123

Warning: mysql_insert_id() [function.mysql-insert-id]: A link to the server could not be established in C:\Users\Jonny\Desktop\projects\xampp\htdocs\phpprojects\lets\ad.php on line 123

解决方案代码:

<?php

$userid = $_SESSION['user_id'];
$price = $ad['price'];
$owner = $ad['owner']; //owner of advertisement

mysqli_close($dbc);
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
    die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($dbc, 'databasename');

//make a bid for this advertisement
$query = "INSERT INTO bids (id, ad, bidder, bid, bidwhen, owner, quantity)
         VALUES (NULL, '$adid', '$userid','$price', now(), '$owner', 1)";
$bidData = mysqli_query($dbc, $query);

$message = $_POST['message']; //message to owner

if ($message != "") { //if message box is not empty insert comment
    $title = $ad['title'];
    $bidid = mysql_insert_id($dbc); //get last id of bid insert and put it into message query for reference

    $query = "INSERT INTO messages (sentto, sentfrom, sentat, message, title, bid)
    VALUES ('$owner', '$userid', now(), '$message', '$title', '$bidid')";
    $messageData = mysqli_query($dbc, $query);
}
?>

传递给mysql_insert_id的参数是数据库连接的资源。 您正在向它提供MySQL查询的结果。 除非您要打开多个数据库连接,否则仅mysql_insert_id()本身应该可以工作。

http://us3.php.net/mysql_insert_id

  1. pygorex1说了什么。 您不想将查询结果传递给* insert_id()
  2. 为什么要使用mysql i _query(),但要使用mysql_insert_id()? 如果您使用的是mysqli,请使用myqli_insert_id

有点晚了,但是我在php手册中看到,界面如下:

  1. int mysql_insert_id ([ resource $link_identifier ] );
  2. int mysqli_insert_id ( mysqli $link )

所以我认为:

  1. 如果您使用的是mysqli风格,则必须使用mysqli_insert_id($link)
  2. 您应该始终将链接标识符传递给mysqli_insert_id函数。

您没有检查mysqli_query的结果,应该这样做:

if (($bidData = mysqli_query($dbc, $query) !== true) {
       printf("Error: %s in query %s\n", $mysqli->error,$query);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM