我尝试设置这样的变量:$test_id = mysql_insert_id($dbc);
因为我想获取插入的最后一行的 id。我能够连接到数据库并插入该行,但下一行(设置 $test_id)它说:supplied argument is not a valid MySQL-Link resource
。有任何想法吗?
这是代码:
$user_id = $_SESSION['user_id'];
$q = "INSERT INTO tests (user_id, title, subject, creation_date) VALUES ('$user_id', '$title', '$subj', NOW())"; //query to insert test details into tests table
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
$test_id = mysql_insert_id($dbc);
if (mysqli_affected_rows($dbc) == 1) {//if the query ran correctly and the test details were added to the database
$q = "INSERT INTO content (test_id, field1) VALUES ($test_id, '$content')"; //query to insert test content into content table
$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
$dbc 是这样定义的:
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASS, DB_NAME);