我是php的绝对初学者,还可以使用sql。因此,为混乱的编码道歉。
我正在尝试将包含值'0004'的数据类型为VARCHAR(4)的变量传递到我在mysql上创建的存储过程中。这是代码:
<html>
<head><title> AmountDue </title><head>
<body>
<h1> Amount Due </h1>
<?php
$guestid = "0004";
$mysqli = new mysqli('host', 'username', 'pass', 'schemaname');
$rs = $mysqli->query( 'CALL FindRoomCost('.$guestid.', @roomCost);');
$rs = $mysqli->query( 'CALL FindFoodCost('.$guestid.', @foodCost);');
$rs = $mysqli->query( 'CALL FindSpaCost('.$guestid.', @spaCost);');
$rs = $mysqli->query( 'SELECT gu.guest_id, bi.invoice_no, gu.first_name,
gu.last_name, @roomCost + @foodCost + @spaCost AS
TotalCost FROM guest gu, bill bi
WHERE gu.guest_id = bi.guest_no
AND gu.guest_id = '.$guestid.';' );
print "<table border=1>";
print "<TR><TH>GuestID</TH><TH>InvoiceNumber</TH><TH>FirstName</TH<TH>LastName</TH><TH>TotalCost</TH></TR>";
while ($r2 = mysqli_fetch_row($rs)) {
print "<tr>";
for ($k=0; $k<count($r2); $k++){
print "<td>" . htmlspecialchars($r2[$k]) . "</td>";
}
print "</tr>";
}
print "</table>";
print "<HR width=85%>";
?>
代码成功显示链接到请求的guestid的访客姓名,ID,发票号。但是,它不会调用我想要的存储过程。虽然如果我在程序的参数中手动输入'0004'而不是$ guestid,存储过程将成功运行并给我TotalCost,这是我想要的结果。我知道我的存储过程没有任何问题,但我会发布它以防万一。任何帮助将不胜感激。
FindRoomCost
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FindRoomCost`
(
IN guestTag VARCHAR(4),
OUT roomCost INT
)
BEGIN
SELECT
(DATEDIFF(res.check_out_date, res.check_in_date))*(count(DISTINCT(ro.room_id)) * (ro.room_rate))
INTO roomCost
FROM room ro, reservation res
WHERE ro.guest_number = res.guest_id
AND ro.guest_number = guestTag;
END
FindFoodCost
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FindFoodCost`
(
IN guestTag VARCHAR(4),
OUT foodCost INT
)
BEGIN
SELECT CASE
WHEN nrc.invoice_no IN (bi.invoice_no)
THEN SUM(DISTINCT(nrc.quantity * sm.menu_price))
ELSE 0
END
INTO foodCost
FROM bill bi, non_room_charge nrc, set_menu sm
WHERE bi.invoice_no = nrc.invoice_no
AND nrc.menu_no = sm.menu_id
AND bi.guest_no = guestTag;
END
FindSpaCost
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FindSpaCost`
(
IN guestTag VARCHAR(4),
OUT spaCost INT
)
BEGIN
SELECT CASE
WHEN nrc.invoice_no IN (bi.invoice_no)
THEN SUM(DISTINCT(nrc.quantity * sp.spa_price))
ELSE 0
END
INTO spaCost
FROM bill bi, non_room_charge nrc, spa_package sp
WHERE bi.invoice_no = nrc.invoice_no
AND nrc.spa_no = sp.spa_id
AND bi.guest_no = guestTag;
END
答案 0 :(得分:1)
删除期间。
$rs = $mysqli->query( 'CALL FindRoomCost('.$guestid.', @roomCost);');
更改为:
$rs = $mysqli->query( 'CALL FindRoomCost('$guestid', @roomCost);');