mysql_insert_id()不起作用

时间:2010-09-24 03:47:00

标签: php

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

4 个答案:

答案 0 :(得分:12)

传递给mysql_insert_id的参数是数据库连接的资源。你正在为它提供MySQL查询的结果。只有mysql_insert_id()本身应该工作,除非您打开多个数据库连接。

http://us3.php.net/mysql_insert_id

答案 1 :(得分:6)

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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

  1. int mysql_insert_id ([ resource $link_identifier ] );
  2. int mysqli_insert_id ( mysqli $link )
  3. 所以,我认为:

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