$mysqli->insert_id wont work

869 views Asked by At

After inserting a new row into my external database, i want to receive back the new (auto-incremented) ID that was created along with the new row.

<?php

$response = array();

// check for required fields
if (isset($_POST['thread']) && isset($_POST['event_name']) && isset($_POST['fb_id']) && isset($_POST['status'])) {

$event_name = $_POST['event_name'];
$thread = $_POST['thread'];
$fb_id = $_POST['fb_id'];
$status = $_POST['status'];

 // include db connect class
require_once __DIR__ . '/db_fb_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO events(event_name, thread) VALUES ('$event_name', '$thread')");

Things are working perfectly fine until here. The row is created inside the events table without further problems. Now i want to get the created ID:

$last_id = $mysqli->insert_id();  

This is where the code stops working. I tried some variations of the insert_id statement, but none seemed to work. What is the correct way here? After assigning $last_id with the latest ID, i would want a second query:

mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO UserEvents(fb_id, event_id, status) VALUES('$fb_id', '$last_id', '$status')");

Im grateful for any kind of help!

4

There are 4 answers

0
Paul On BEST ANSWER

OOP style insert_id is a property, not a function:

$last_id = $mysqli->insert_id;  
// or, since your mysqli object seems to be in $GLOBALS["___mysqli_ston"]
$last_id = $GLOBALS["___mysqli_ston"]->insert_id;

It looks like you're not actually using the object-oriented style of mysqli. If you want to be consistent and use procedural style everywhere, you should use:

$last_id = mysqli_insert_id( $GLOBALS["___mysqli_ston"] );
0
GautamD31 On

Can you try in Object oriented style like

$result = mysqli_query("INSERT INTO events(event_name, thread) VALUES ('".$event_name."', '".$thread."')");

And you will get the last insert Id with

$mysqli->insert_id;
0
Gvidas On

There is two ways to achieve it:

$last_id = mysqli_insert_id($GLOBALS["___mysqli_ston"]); 

or as Paulpro answered:

$last_id = $mysqli->insert_id;
0
YaBCK On

Here an example of creating a table and showing record id. Practise is always good when you don't know what the error is

<?php

$mysqli = new mysqli("localhost", "user", "pass", "db");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

$mysqli->query("CREATE TABLE myTable LIKE tableExample");

$query = "INSERT INTO myTable VALUES (NULL, 'Test1', 'Test2', 'Test3', 'Test4')";
$mysqli->query($query);

/* show table id */
printf ("New Record has id %d.\n", $mysqli->insert_id);

/* drop table */
$mysqli->query("DROP TABLE myTable");

/* close connection */
$mysqli->close();
?>

The reason the insert_id() isn't working is because insert_id is not a function it's a property. So you want to use insert_id not insert_id()

As Paulpro points out there is two ways of doing this: Paulpro answer

OOP style insert_id is a property, not a function:

$last_id = $mysqli->insert_id; // or, since your mysqli object seems to be in $GLOBALS["___mysqli_ston"]

$last_id = $GLOBALS["___mysqli_ston"]->insert_id;

It looks like you're not actually using the object-oriented style of mysqli. If you want to be consistent and use procedural style everywhere, you should use:

$last_id = mysqli_insert_id( $GLOBALS["___mysqli_ston"] );