php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69046 Die(MySQLi->Insert_Id) Doesn't Work
Submitted: 2015-02-12 22:07 UTC Modified: 2015-02-12 22:34 UTC
From: matthewcomp at hotmail dot com Assigned:
Status: Not a bug Package: MySQLi related
PHP Version: Irrelevant OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: matthewcomp at hotmail dot com
New email:
PHP Version: OS:

 

 [2015-02-12 22:07 UTC] matthewcomp at hotmail dot com
Description:
------------
I have a script which is supposed to insert a query into a database, and output the insert ID if the operation is successful. (It's a very simple piece of an AJAX framework.)

My MySQL handler is called $sql.

If I put:
die($sql->insert_id);

The output is empty. I expect the Insert ID to be outputted before the script ends.

This code works fine:
echo $sql->insert_id;
die();

Test script:
---------------
  $sql = new mysqli($db_host, $db_user, $db_pass, $db_name);
  if ($sql->connect_error) {
    die('Connect Error ('.$sql->connect_errno.') '.$sql->connect_error);
  }

  $day = $_REQUEST['day'];
  $content = $_REQUEST['content'];

  if ($day != "-1" && $day != "0" && $day < 1) {
    die("Invalid Day!");
  }
  
  if (strlen($content) < 10) {
    die("Content Too Short!");
  } else if (strlen($content) > 255) {
    die("Content Too Long!");
  }
  
  $query = "INSERT INTO task (name, day_id, type, last, time) VALUES ('".$sql->escape_string($content)."', ".($day-0).", 0, ".time().", ".time().");";
  //echo $query;
  if ($sql->query($query)) {
    //Return Item ID:
    die($sql->insert_id);
  } else {
    die("MySQL Error! ".$query." - ".$sql->error);
  }

Expected result:
----------------
Empty page. (No output.)

Actual result:
--------------
Insert ID for query. (ie 213.)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-12 22:33 UTC] phpmpan at mpan dot pl
NaB

Please read documentation for `die` (or `exit`), especially on meaning of its argument depending on argument's type.
 [2015-02-12 22:34 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-02-12 22:34 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Remembering that die() is an alias of exit(),

http://php.net/manual/en/function.exit.php
>If status is an integer, that value will be used as the exit status and not
>printed.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Mon May 23 23:05:47 2022 UTC