简体   繁体   English

变量未传递到存储过程

[英]Variable not passing into stored procedure

I'm an absolute beginner at php and okay with sql. 我是php的绝对初学者,可以使用sql。 So, due apologies for the messy coding. 因此,为乱码表示歉意。

I'm currently trying to pass a variable which contains the value '0004', data type VARCHAR(4), into a stored procedure which I created on mysql. 我目前正在尝试将包含值'0004',数据类型VARCHAR(4)的变量传递到在mysql上创建的存储过程中。 Here is the code: 这是代码:

<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%>";
?>

The code successfully displays the guest name, ID, invoice number that is linked to the requested guestid. 该代码成功显示了链接到请求的来宾ID的来宾名称,ID,发票编号。 However, it does not call the stored procedures that I want. 但是,它不会调用我想要的存储过程。 Though if I manually input '0004', instead of $guestid, in the procedure's parameters, the stored procedures will successfully run and give me the TotalCost which is the result I want. 尽管如果我在过程的参数中手动输入'0004'而不是$ guestid,则存储过程将成功运行并为我提供TotalCost,这是我想要的结果。 I know there's nothing wrong with my stored procedures but I'll just post it just in case. 我知道我的存储过程没有任何问题,但是我只是将其发布以防万一。 Any help will be greatly appreciated. 任何帮助将不胜感激。

FindRoomCost 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 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 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

Remove the periods. 删除句点。

$rs = $mysqli->query( 'CALL FindRoomCost('.$guestid.', @roomCost);');

CHANGE TO: 改成:

$rs = $mysqli->query( 'CALL FindRoomCost('$guestid', @roomCost);');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM