medvedo medvedo - 3 months ago 9x
SQL Question

Trying to reach the returned ID I get after I insert value but mysqli_insert_id() returns null


$value = json_decode(file_get_contents('php://input'));
$mysql_pekare= new mysqli ("serv", "user","pass", "db");

if(!empty($value)) {
$stmt = $mysql_pekare->prepare("INSERT INTO users (`username`, `password`) VALUES(?,?)");

$stmt->bind_param("ss", $value->username, $value->password);

if(!empty($stmt)) {

$contacts = array();
$id = mysqli_insert_id();
$contact = array("objectId" => ($id));
array_push($contacts, $contact);
echo json_encode(array('results' => $contacts), JSON_PRETTY_PRINT);



Now when I insert my info from my frontend the value (username + password) gets added into MySQL just fine with a unique ID but I do not get the returned ID, currently I get it like this:

"results": [
"objectId": null


Your $id is most likely null or an error not sure...

//This is for procedural and needs a link
$id = mysqli_insert_id(); 
//should be
$id = mysqli_insert_id($mysql_pekare); 
//$mysql_pekare I am assuming is your connection...

//however you are using oo (or I think) so should be