Bug #69046 [Com]: Die(MySQLi->Insert_Id) Doesn't Work
| From: | phpmpan at mpan dot pl | Date: | Thu, 12 Feb 2015 22:33:05 +0000 |
| Subject: | Bug #69046 [Com]: Die(MySQLi->Insert_Id) Doesn't Work | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-190639@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69046&edit=1
ID: 69046
Comment by: phpmpan at mpan dot pl
Reported by: matthewcomp at hotmail dot com
Summary: Die(MySQLi->Insert_Id) Doesn't Work
Status: Open
Type: Bug
Package: MySQLi related
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
NaB
Please read documentation for
die (or exit), especially on meaning of its
argument depending on argument's type.
Previous Comments:
------------------------------------------------------------------------
[2015-02-12 22:07:10] 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.)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69046&edit=1