Ajitha Malkanthi
Ajitha Malkanthi

Reputation: 107

Getting the AUTO_INCREMENT value from php(not working)

I have a simple mysqli code to select the current AUTO_INCREMENT value in a table named bookings.

After the code executes, nothing happens.I do not get any output in the screen.

Here is the code.

if ($result = mysqli_query($conn, "SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = titan3d AND TABLE_NAME = bookings", MYSQLI_USE_RESULT)) {
    if (!mysqli_query($conn, "SET @a:='this will not work'")) {
        printf("Error: %s\n", mysqli_error($conn));
    }
    myslqi_stmt_fetch_assoc($result);
    var_dump($result);
}

Is there something wrong with this code.Can somebody sort it out?

Upvotes: 1

Views: 109

Answers (1)

Barmar
Barmar

Reputation: 781721

You have a syntax error in the query, you didn't quote the strings. And then you need to fetch the result row.

if ($result = mysqli_query($conn, "SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'titan3d' AND TABLE_NAME = 'bookings'", MYSQLI_USE_RESULT)) {
    $row = mysqli_fetch_assoc($result);
    echo "Auto-increment is {$row['AUTO_INCREMENT']}";
} else {
    echo mysqli_error($conn);
}

Upvotes: 2

Related Questions