系统/开发规格;
- Windows 7 Ultimate x64 SP1 +所有更新 PHP Version 5.5.15 Non-Thread Safe (x64测试版)MySQL Server 5.6 (x64)Php mysqli
我正在执行一个存储过程,它将用户名和密码插入到AUTO_INCREMENT id INT(11) PK字段的表中。
PROCEDURE `user_account_create`(IN userName VARCHAR(32), IN userPasskey VARCHAR(254))
BEGIN
START TRANSACTION;
INSERT INTO user_account (`name`, passkey) VALUES (userName, userPasskey);
IF (ROW_COUNT() = 1) THEN COMMIT; ELSE ROLLBACK; END IF;
SELECT ROW_COUNT() AS affected_rows; -- Used for PHP mysqli's Connection and Statement affected_rows, and num_rows (Statement) fields.
END
简单地说,我已经把mysqli包装在我自己的类中了;
namespace DataAccessBroker {
final class MySqliDb {
private $conn;
public function __construct($dbHost, $dbUser, $dbPass, $dataBase) {
$this->conn = new mysqli($dbHost, $dbUser, $dbPass, $dataBase);}
public function ExecuteStatement($cmdText, array $paramValue = null) {
$affected = -1;
$stmt = $this->CreateStatement($cmdText, paramValue);
$stmt->execute();
// echo 'insert_id' . $this->conn->insert_id;
$stmt->store_result();
$affected = $stmt->affected_rows;
stmt->close();
return $affected;
}
// ... other functions that utilse CreateStatement below
private function CreateStatement($cmdText, array $paramValue = null) {
$stmt = $this->conn->prepare($cmdText);
if ($paramValue !== null) {
$params = [];
foreach ($paramValue as $p => &$v) {$params[$p] = &$v;}
call_user_func_array([$stmt, 'bind_param'], $params);
}
return $stmt;
}
} // class
} // namespace
在index.php页面上测试;
use DataAccessBrokerMySqliDb as mysqldb;
$db = new mysqldb('127.0.0.1', 'root', '', 'thedb');
$types = 'ss'; $user_name = 'its_me'; $pass_key = 'a-hashed-password';
echo 'Affected Rows: ' . $db->ExecuteStatement('CALL user_account_create(?,?)', [$types, $user_name, $pass_key]);
将产生影响行:1。
插入成功。我也需要这个命令的插入id,但是mysqli连接和语句的insert_id都是0。
连接的var_dump:
object(mysqli)#2 (19) {
["affected_rows"] => int(1)
["client_info"] => string(79) "mysqlnd 5.0.11-dev - 20120503 - $Id: xxx$" ["client_version"] => int(50011)
["connect_errno"] => int(0) ["connect_error"] => NULL
["errno"] => int(0) ["error"] => string(0) "" ["error_list"] => array(0) { }
["field_count"] => int(1)
["host_info"] => string(20) "127.0.0.1 via TCP/IP" ["info"] => NULL
["insert_id"] => int(0)
["server_info"] => string(6) "5.6.20" ["server_version"] => int(50620)
["stat"] => NULL
["sqlstate"] => string(5) "HY000"
["protocol_version"] => int(10)
["thread_id"] => int(6)
["warning_count"] => int(0)}
var_dump语句:
object(mysqli_stmt)#3 (10) {
["affected_rows"] => int(1)
["insert_id"] => int(0)
["num_rows"] => int(1)
["param_count"] => int(6)
["field_count"] => int(1)
["errno"] => int(0) ["error"] => string(0) "" ["error_list"] => array(0) { }
["sqlstate"] => string(5) "00000"
["id"] => int(1)}
有趣的是,有一个字段"id",这是返回所需的id,从字段id
在我的表。有没有人知道为什么insert_id返回0.
谢谢njc
尝试使用变量来编写存储过程,而不是两次调用row_count()
:
PROCEDURE `user_account_create`(IN userName VARCHAR(32), IN userPasskey VARCHAR(254))
BEGIN
START TRANSACTION;
INSERT INTO user_account (`name`, passkey) VALUES (userName, userPasskey);
IF ((@rc := ROW_COUNT) = 1) THEN COMMIT; ELSE ROLLBACK; END IF;
SELECT @rc AS affected_rows; -- Used for PHP mysqli's Connection and Statement affected_rows, and num_rows (Statement) fields.
END;
我认为第二个调用是指if
语句。