MySQL 过程将空结果返回给 PHP
Posted
技术标签:
【中文标题】MySQL 过程将空结果返回给 PHP【英文标题】:MySQL procedure returns empty result to PHP 【发布时间】:2017-01-10 07:29:46 【问题描述】:我有一个问题,我编写了一个存储过程,将数据插入到一堆表中,然后最后通过选择它返回和 id。当我在 phpMyAdmin 中执行存储过程时,插入数据并返回 ID。
当使用 mysqli 从 PHP 执行过程时,数据被插入到表中,但是当我检查结果以检索新 id 时,结果似乎是空白的。这里的任何帮助将不胜感激。我不确定该过程是否在返回包含具有新 ID 的行的结果之前返回空结果。 phpMyAdmin 中的执行结果中的空白行对我来说是不是有点麻烦?
例程sp_createEvent
的执行结果
事件标识 30
存储过程:
DELIMITER $$
CREATE PROCEDURE `sp_createEvent` (IN varEventName varchar(200), IN varUserID INT, IN varPrintingRequested INT, IN varEventCode BIGINT, IN varActiveFrom DATETIME, IN varActiveTo DATETIME, IN varLimitGuestPhotos BIT)
LANGUAGE SQL
DETERMINISTIC
SQL SECURITY DEFINER
COMMENT 'A procedure'
BEGIN
DECLARE albumname VARCHAR(50);
DECLARE albumslug VARCHAR(50);
DECLARE usertopalbum INT;
DECLARE eventalbumid INT;
DECLARE fullphotosgalleryid INT;
DECLARE photostripgalleryid INT;
DECLARE eventid INT;
DECLARE CONTINUE HANDLER FOR NOT FOUND BEGIN END;
START TRANSACTION;
SELECT
CONCAT(u.`display_name`, " - ",varEventName),
REPLACE(LOWER(CONCAT(RTRIM(u.`display_name`), "-",varEventName)), " ", "-"),
uasc.`album_id`
INTO
albumname,
albumslug,
usertopalbum
FROM `wp_users` u
LEFT JOIN `wp_pocketbooth_useralbumshortcode` uasc on uasc.`wp_user_id` = u.ID
WHERE u.`ID` = varUserID and
uasc.`album_id` is not null;
INSERT INTO `wp_bwg_album`(`id`, `name`, `slug`, `description`, `preview_image`, `random_preview_image`, `order`, `author`, `published`)
VALUES (NULL,albumname,albumslug,'smartbooth.co.za Album','','','1','1','1');
SET eventalbumid = LAST_INSERT_ID();
INSERT INTO `wp_bwg_gallery`(`id`, `name`, `slug`, `description`, `page_link`, `preview_image`, `random_preview_image`, `order`, `author`, `published`, `gallery_type`, `gallery_source`, `autogallery_image_number`, `update_flag`)
VALUES (NULL, CONCAT(albumname, ' (Full Photos)'), CONCAT(albumslug, '-full-photos'),'smartbooth.co.za Gallery','','','','','1','1',1,'','12','');
SET fullphotosgalleryid = LAST_INSERT_ID();
INSERT INTO `wp_bwg_gallery`(`id`, `name`, `slug`, `description`, `page_link`, `preview_image`, `random_preview_image`, `order`, `author`, `published`, `gallery_type`, `gallery_source`, `autogallery_image_number`, `update_flag`)
VALUES (NULL, CONCAT(albumname, ' (Photo Strips)'), CONCAT(albumslug, '-photo-strips'),'smartbooth.co.za Gallery','','','','','1','1',1,'','12','');
SET photostripgalleryid = LAST_INSERT_ID();
INSERT INTO `wp_bwg_album_gallery`(`id`, `album_id`, `is_album`, `alb_gal_id`, `order`)
VALUES (NULL,eventalbumid,'0',fullphotosgalleryid,'1');
INSERT INTO `wp_bwg_album_gallery`(`id`, `album_id`, `is_album`, `alb_gal_id`, `order`)
VALUES (NULL,eventalbumid,'0',photostripgalleryid,'1');
INSERT INTO `wp_pocketbooth_events` (`eventid`, `eventname`, `printingrequested`, `printimages`, `eventcode`, `toplevelalbumid`,`activefrom`,`activeto`, `limitguestphotototime`)
VALUES (NULL, varEventName, varPrintingRequested, 0, varEventCode, eventalbumid, varActiveFrom, varActiveTo, varLimitGuestPhotos);
SET eventid = LAST_INSERT_ID();
INSERT INTO `wp_pocketbooth_eventsubscriptions` (`subscriptionid`, `userid`, `eventid`, `isowner`)
VALUES (NULL, varUserID, eventid, 1);
commit;
select eventid;
END $$
DELIMITER ;
PHP 代码:
<?php
include 'connection.php';
$userid = $_POST['userid'];
$eventname = $_POST['eventname'];
$printingrequested = $_POST['printingrequested'];
$eventcode = $_POST['eventcode'];
$activefrom = $_POST['activefrom'];
$activeto = $_POST['activeto'];
$limitphotos = $_POST['limitphotos'];
$sql = "CALL `sp_createEvent` ('$eventname' , '$userid' , '$printingrequested' , '$eventcode' , '$activefrom' ,'$activeto', '$limitphotos');";
$res = $connection->query($sql);
if (!$res)
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
else
$data = array();
while($row = mysql_fetch_array($res))
$data[] = $row['eventid'];
echo json_encode($data);
@mysqli_close($conn);
?>
【问题讨论】:
【参考方案1】:而不是 SET eventid = LAST_INSERT_ID(); 写
select LAST_INSERT_ID() into eventid;
【讨论】:
以上是关于MySQL 过程将空结果返回给 PHP的主要内容,如果未能解决你的问题,请参考以下文章
mysql存储过程 把SQL语句返回结果赋给一个变量,该SQL语句返回的结果不止一条,该怎么写,新手,求指教!
通过数据库上下文将空 DataTable 传递给 SQL Server 存储过程