我有一个将数据插入数据库的存储过程(从现在开始为 SP)(SaveClient,见下文)。当 SP 完成后,我将 PHP 页面重定向到另一个列出条目的 PHP 页面(FetchObjectList,见下文)。在我重新加载/刷新页面之前,该列表不会返回新创建的记录。
存储过程COMMIT
最后有一个,我在调用 SP 后关闭 PHP 代码中的数据库连接,并检查错误但没有出错。
该页面本身返回一个 200 状态码,这意味着它没有被缓存,因此也不能与浏览器相关。
当前的解决方法是sleep(1)
在 PHP 代码中,但是当代码上线时,我不知道它是否足够。我当然宁愿让 MySQL 提供正确的结果集。
编辑:我正在使用 PHP 的 MySQLi 对象接口,知道可能会有用。;)
我的开发计算机安装了 PHP 5.2.17、MySQL 5.0.51a (InnoDB) 和 Apache 2.2.17,并在 Windows 7 x64 上运行。
更新
将以下行添加CALL FetchObjectList('client_tbl', NULL, NULL, 1, 'client_tbl.name ASC', NULL, NULL);
到 SaveClient 的末尾。结果集中在呈现的结果集中没有新创建的客户端。
更新 2
我尝试使用这里SQL_NO_CACHE
看到的但无济于事。
我现在将直接在 PHP 中尝试相同的 SQL,而不是调用 SP。
9 月 3 日至 20 日更新
到目前为止,我已经尝试了任何合理的答案/评论,但没有任何运气。我今天尝试更新我的 PHP 和 MySQL 版本(因为我今天了解到实时服务器将在 PHP 5.3.something 和 MySQL 5.1.something 上运行)但没有让它工作。我需要更新 PHP 以获得更新的php_mysqli.dll
/libmysql.dll
因为我得到的仅支持 5.0.51a 并且可能存在我的问题,因为实际数据库中没有任何工作。我尝试了libmysql.dll
从 MySQL 安装无济于事。
请注意,我还更改了包含的 PHP 代码,因为我实际上复制了错误的调用 theuser_tbl
而不是 the 的代码client_tbl
,并且还简化了它(删除了多查询),但结果仍然相同。
我不知道赏金会发生什么,如果它恢复给我,我会再次添加它。
存储过程 SaveClient
DELIMITER //
DROP PROCEDURE IF EXISTS work.SaveClient//
CREATE PROCEDURE work.SaveClient(
IN ObjectID INT,
IN UserID INT,
IN ClientName VARCHAR(60),
IN VersionFrom DATETIME,
IN VersionTo DATETIME)
root:BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
/*
Default values ---------------------------------------------------------------------------------------------------------
*/
# Used to block INSERT/UPDATEs
SET @DoChanges = TRUE;
SET @Fields = '*';
SET @Version = NULL;
SET @UserVersion = NULL;
SET @DateNow = NOW();
SET @VersionActive = CONCAT(
'( ( NOW() BETWEEN ',
'version_from AND ',
'version_to ) OR ( ',
'version_from < NOW() AND ',
'version_to IS NULL ) )'
);
IF VersionFrom IS NULL THEN
SET VersionFrom = @DateNow;
END IF;
/*
Search for client ------------------------------------------------------------------------------------------------------
*/
IF ObjectID IS NOT NULL THEN
SET @Client = CONCAT(
'SELECT version INTO @Version FROM client_tbl WHERE object_id = ',
ObjectID,
' AND ',
@VersionActive
);
PREPARE stmt FROM @Client;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
# Check if there are any changes
IF @Version IS NOT NULL THEN
SELECT name INTO @Name FROM client_tbl WHERE name = ClientName AND version = @Version;
IF @Name = ClientName THEN
SET @errorMsg = "Duplicate entry";
SET @errorCode = "S0000002";
SELECT @errorCode, @errorMsg;
LEAVE root;
END IF;
END IF;
END IF;
/*
Search for user ---------------------------------------------------------------------------------------------------------
*/
# Create this as a function
IF UserID IS NOT NULL THEN
SET @User = CONCAT(
'SELECT version INTO @UserVersion FROM user_tbl WHERE object_id = ',
UserID,
' AND ',
@VersionActive
);
PREPARE stmt FROM @User;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
IF @UserVersion IS NULL THEN
SET @errorMsg = "User is missing";
SET @errorCode = "U0000099";
SELECT @errorCode, @errorMsg;
LEAVE root;
END IF;
/*
Add the client ---------------------------------------------------------------------------------------------------------
*/
# Close the current version
IF @Version IS NOT NULL THEN
IF @DoChanges = TRUE THEN
CALL UpdateVersion(
ObjectID,
UserID,
@Version,
@DateNow,
'client_tbl'
);
SET @Version = @Version + 1;
END IF;
ELSE
SET @Version = 1;
END IF;
IF @DoChanges = TRUE THEN
IF ObjectID IS NULL THEN
INSERT INTO
object_tbl
(
object_class_id,
created,
created_by
)
VALUES(
2,
NOW(),
UserID
)
;
SET ObjectID = LAST_INSERT_ID();
END IF;
INSERT INTO
client_tbl
(
object_id,
version,
version_from,
version_to,
changed,
changed_by,
name
)
VALUES(
ObjectID,
@Version,
VersionFrom,
NULL,
@DateNow,
UserID,
ClientName
)
;
END IF;
COMMIT;
END //
DELIMITER ;
存储过程 FetchObjectList
DELIMITER //
DROP PROCEDURE IF EXISTS work.FetchObjectList//
CREATE PROCEDURE work.FetchObjectList(
IN ObjectType VARCHAR(60),
IN ObjectSubType VARCHAR(60),
IN ObjectSubID INT,
IN IsActive INT,
IN OrderBy VARCHAR(100),
IN SetStart INT,
IN MaxResults INT)
root:BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
# Allow the "JSON" output be a max of 8kb
SET GLOBAL group_concat_max_len = 8096;
/*
Default values ---------------------------------------------------------------------------------------------------------
*/
SET @Fields = '*';
SET @VersionWhere = '1'; # Get everything
SET @Special = '';
SET @OrderBy = '';
SET @SetStart = '';
SET @MaxResults = '';
SET @JoinIn = '';
IF IsActive = 1 THEN
SET @VersionWhere = CONCAT(
'( NOW() BETWEEN ',
ObjectType,
'.version_from AND ',
ObjectType,
'.version_to OR ( ',
ObjectType,
'.version_from < NOW() AND ',
ObjectType,
'.version_to IS NULL ) )'
);
END IF;
IF OrderBy != '' THEN
SET @OrderBy = CONCAT(
'ORDER BY ',
OrderBy
);
END IF;
/*
Specials for each type -------------------------------------------------------------------------------------------------
*/
/*
- Clients ------------
*/
IF ObjectType = 'client_tbl' THEN
SET @Fields = '
*,
client_tbl.object_id AS object_id,
(
SELECT
COUNT(*) AS Total
FROM
client_user_privilege_tbl cup
WHERE
cup.client_id = client_tbl.object_id
) AS usercount
';
END IF;
/*
- Configuration ------------
*/
IF ObjectType = 'configuration_tbl' THEN
SET @Fields = '
*
';
END IF;
/*
Add upp the query to run -----------------------------------------------------------------------------------------------
*/
SET @Query = CONCAT(
'SELECT ',
@Fields,
' FROM ',
ObjectType,
' ',
@JoinIn,
' WHERE ',
@VersionWhere,
' ',
@Special,
@OrderBy
);
PREPARE stmt FROM @Query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
COMMIT;
END //
DELIMITER ;
PHP 代码片段(9 月 20 日更新)
$query = "CALL FetchObjectList('client_tbl', NULL, NULL, 1, NULL, NULL, NULL)";
addTrace($query);
$rs = $db->query($query);
if( $rs ) {
addTrace('Query done -> Results: ' . $rs->num_rows);
while($r = $rs->fetch_assoc()){
$fetchArray[] = $r;
}
$count = $rs->num_rows;
$rs->close();
$db->next_result();
} else {
addTrace('Query failed -> ' . $db->error);
flushTrace();
exit;
}