我写了一个存储过程。我没有得到我正在寻找的数据,我想知道如何逐行查看输出。我在 PLSQL 方面拥有丰富的经验,因此我通常只会写入网页并查看结果。有什么方法可以使用下面的存储过程并使用 php 将其打印为 html 或使用 PHPMyAdmin 来查看错误所在。我确信这很简单,我在程序中遗漏了一些东西,但 3 小时后我不知道。我正在盲目自学 PHP 和 MySQL。
BEGIN
DECLARE v_szAwayTeam varchar(5);
DECLARE v_szHomeTeam varchar(5);
DECLARE v_nPointsAway INTEGER DEFAULT 0;
DECLARE v_nPointsHome INTEGER DEFAULT 0;
DECLARE v_nPointsFor INTEGER DEFAULT 0;
DECLARE v_nPointsAgainst INTEGER DEFAULT 0;
DECLARE v_finished INTEGER DEFAULT 0;
DECLARE curSchedule CURSOR
FOR
SELECT szAway, szHome, nPointsAway, nPointsHome FROM schedule_master WHERE
szGame = 'R' ORDER BY dtGame, dtTime;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_finished = 1;
OPEN curSchedule;
get_Sched: LOOP
FETCH curSchedule INTO v_szAwayTeam, v_szHomeTeam, v_nPointsAway,
v_nPointsHome;
IF (v_nPointsAway < v_nPointsHome) THEN
SELECT nPointsFor INTO v_nPointsFor FROM standings_master WHERE
idTeam = v_szHomeTeam;
SET v_nPointsFor = (v_nPointsHome + v_nPointsFor);
UPDATE standings_master SET nPointsFor = v_nPointsFor WHERE
idTeam = v_szHomeTeam;
SELECT nPointsAgainst INTO v_nPointsAgainst FROM
standings_master WHERE idTeam = v_szAwayTeam;
SET v_nPointsAgainst = (v_nPointsAway + v_nPointsAgainst);
UPDATE standings_master SET nPointsAgainst = v_nPointsAgainst
WHERE idTeam = v_szAwayTeam;
COMMIT;
ELSEIF (v_nPointsHome < v_nPointsAway) THEN
SELECT nPointsFor INTO v_nPointsFor FROM standings_master WHERE
idTeam = v_szAwayTeam;
SET v_nPointsFor = (v_nPointsAway + v_nPointsFor);
UPDATE standings_master SET nPointsFor = v_nPointsFor WHERE
idTeam = v_szAwayTeam;
SELECT nPointsAgainst INTO v_nPointsAgainst FROM
standings_master WHERE idTeam = v_szHomeTeam;
SET v_nPointsAgainst = (v_nPointsHome + v_nPointsAgainst);
UPDATE standings_master SET nPointsAgainst = v_nPointsAgainst
WHERE idTeam = v_szHomeTeam;
COMMIT;
END IF;
IF v_finished = 1 THEN
LEAVE get_Sched;
END IF;
END LOOP get_Sched;
CLOSE curSchedule;
END
最佳答案
我根本不会在 MySQL 中为此使用存储过程。
我知道您来自 Oracle PL/SQL 领域,它为基于存储过程的 Web 应用程序提供了丰富的开发环境。
与 Oracle 强大的 Web 开发环境相比,MySQL 存储过程简直就是垃圾。
- MySQL 存储过程没有包。
- 没有程序调试器。
- 不支持网页输出。
- 没有标准的程序库。
- 没有编译过程。它们会在您每次使用时按需编译。
大多数 MySQL 开发人员使用补充脚本语言编写应用程序的效率更高,而不是使用数据库包和过程进行开发。 PHP 是一种流行的脚本语言,但还有其他语言。
我认为存储过程示例的逻辑可以归结为两个更简单的 UPDATE 语句。您不需要使用光标。
下面是一个示例(虽然我还没有测试过),展示了 PHP 及其数据库 API(称为 PDO)的使用。既然您在 2017 年学习 PHP,请帮自己一个忙:跳过过时的 mysql 和 mysqli API。
这里有一个非常好的 PDO 教程:https://phpdelusions.net/pdo
这里有一个非常好的网站,提供一般 PHP 最佳实践:http://www.phptherightway.com
<?php
// You need to set your DSN and $user and $password.
// I recommend putting them in a config file, and read it with
// http://php.net/manual/en/function.parse-ini-file.php
// Remember to put the config file *outside* your HTTP
// document root, to avoid the risk of any client reading it!
$config = parse_ini_file('/path/to/config.ini');
try {
$db = new PDO($config['dsn'], $config['user'], $config['password']);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
} catch (PDOException $e) {
error_log('Connection failed: ' . $e->getMessage());
die('Sorry, the database failed. Please contact the webmaster.');
}
$sql = "
UPDATE standings_master AS m
JOIN schedule_master AS s ON m.idTeam = s.szHome
SET m.nPointsFor = m.nPointsFor + IF(s.nPointsAway < s.nPointsHome, s.nPointsHome, 0),
m.nPointsAgainst = m.nPointsAgainst + IF(s.nPointsAway > s.nPointsHome, s.nPointsAway, 0)
WHERE s.szGame = 'R'";
$db->exec($sql);
$sql = "
UPDATE schedule_master AS s
JOIN standings_master AS m ON m.idTeam = s.szAway
SET m.nPointsFor = m.nPointsFor + IF(s.nPointsAway > s.nPointsHome, s.nPointsAway, 0),
m.nPointsAgainst = m.nPointsAgainst + IF(s.nPointsAway < s.nPointsHome, s.nPointsHome, 0)
WHERE s.szGame = 'R'";
$db->exec($sql);
然后要显示结果,您需要使用带有结果集的查询,例如SELECT
:
$sql = "
SELECT idTeam, PointsFor, PointsAgainst
FROM standings_master
ORDER BY PointsFor DESC";
$stmt = $db->query($sql);
$results = $stmt->fetchAll();
?>
<table>
<tr>
<th>Team</th>
<th>Points For</th>
<th>Points Against</th>
</tr>
<?php
foreach ($results AS $row) {
?>
<tr>
<td><?= $row['idTeam'] ?></td>
<td><?= $row['PointsFor'] ?></td>
<td><?= $row['PointsAgainst'] ?></td>
</tr>
<?php
}
?>
</table>
关于PHP MySQL 打印光标到网页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46984842/