0

嗨,我用多个查询复制一行,并希望从结果中获取 mysql_insert_id()。复制工作正常,但我似乎无法从中获取最后一个 id。

这是我的代码:

    $mysqli = new mysqli("localhost", "xxxx", "xxxx", "xxxx");

if ($mysqli->connect_errno) {
    echo($error_page_header);
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    echo($error_page_footer);
    exit;
}

$sql = "CREATE TEMPORARY TABLE tmp ENGINE=MEMORY SELECT * FROM test_table WHERE id=1; ";
$sql.= "UPDATE tmp SET id=NULL; ";
$sql.= "INSERT INTO test_table SELECT * FROM tmp; ";
$sql.= "DROP TABLE tmp; ";

if (!$mysqli->multi_query($sql)) {
    echo($error_page_header);
    echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
    echo($error_page_footer);
    exit;

} do {
    if ($res = $mysqli->store_result()) {
        var_dump($res->fetch_all(MYSQLI_ASSOC));
        $res->free();
    }

} while ($mysqli->more_results() && $mysqli->next_result());    

echo( 'id: ' . mysql_insert_id() ); // prints 0
4

1 回答 1

1

不要混淆..ext/mysql并且mysqli不兼容并且不能相互使用。使用$mysqli->insert_id(它是每个连接)

于 2013-02-27T23:57:55.373 回答