【问题标题】:PHP MySQL Print Cursor to web page?PHP MySQL打印光标到网页?
【发布时间】:2017-10-27 23:43:46
【问题描述】:

我写了一个存储过程。我没有得到我正在寻找的数据,我想知道如何逐行查看输出。我在 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

【问题讨论】:

  • 我认为你可以声明临时表,将数据插入其中,最后在你的过程结束时返回临时表作为结果

标签: php mysql stored-procedures cursor


【解决方案1】:

我根本不会在 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>

【讨论】:

    猜你喜欢
    • 2014-05-14
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2011-04-12
    相关资源
    最近更新 更多