PDO SELECT未完成或引发异常

时间:2019-07-10 20:06:26

标签: php mysql

我正在创建一个页面,该页面通过将事务ID插入链接来查询MySQL数据库以创建Web链接。这很简单,因为我需要的只是来自用户的特定ID,然后单击“提交”。从那里,它应该查询并获取我需要的结果交易ID。

我正在具有PHP 7.2和Apache 2的Ubuntu服务器上运行此程序。我正在另一台也运行Ubuntu服务器的计算机上连接到MySQL服务器。我有一个PDO PHP类,在使用之前,它可以帮助我建立连接和准备语句,创建查询等。我可以将其设置为创建与MySQL服务器的连接的地步,但我没有得到返回任何结果,我看不到任何异常。

<?php
    include('databaseConnections.php');
?>
<!DOCTYPE html>
<html lang="en">
<!--44656469636174656420746f20526179204d61686c6b65-->
<head>
    <meta charset="UTF-8">
    <title>Link Generator</title>
</head>

<body>
    <div class="content">
        <form name="query" id="mainForm" method="post" action="">
            <input required type="text" name="claimNum" id="claimNum" size="12" maxlength="25" placeholder="Claim Number" value=''/> &nbsp;
            <input type="submit" name="btnSubmit" id="btnSubmit" value="Get Link"/> &nbsp;
        </form>
        <?php
            function query() {
                try {
                    $getTransactID = new DatabaseConnection(); //Create new connection.
                    $getTransactID->query('SELECT transactionId FROM Claims WHERE claimNumber = :claimNum');
                    $getTransactID->bind(':claimNum', $_POST["claimNum"]);
                    $row = $getTransactID->fetch(); //Run query and return results.
                    $getTransactID = null; //Close the connection.

                    //Right now, I'm trying to get the result to show up.
                    echo '\n\n\n<pre>';
                    print_r($row);
                    echo '</pre>';

                } catch (Exception $e) {
                    echo '
                    <div id="failure" class="subcontent">
                        <h1>Link Creation Error</h1>
                        <div>
                            '.$e.'
                            <br>
                            <p>Unable to get record from database. Contact an administrator for assistance.</p>
                            <br>
                        </div>
                    </div>';
                }
            }

            if (isset($_POST["claimNum"])){
                query();
            }
        ?>
</body>

<?php
class DatabaseConnection
{
    private $mysqlConnString = 'mysql:host=xx.xx.x.xxx;dbname=data;charset=utf8';
    private $mysqlUser = 'userAcct';
    private $mysqlPassword = 'xxxxxxxxx';
    private $connection;
    private $error;
    private $stmt;

    public function __construct()
    {
        try {
            $this->connection = new PDO($this->mysqlConnString, $this->mysqlUser, $this->mysqlPassword);
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (Exception $e) {
            $this->error = $e->getMessage();
            error_log($e);
        }
    }

    public function setQuery($query)
    {
        $this->stmt = $this->connection->prepare($query);
    }

    public function executeQuery()
    {
        return $this->stmt->execute();
    }

    public function returnResults()
    {
        $this->executeQuery();
        return $this->stmt->fetch(PDO::FETCH_OBJ);
    }

    public function fetch()
    {
        $this->executeQuery();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function bind($param, $value, $type = null)
    {
        if (is_null($type)) {
            switch (true) {
                case is_int($value):
                    $type = PDO::PARAM_INT;
                    break;
                case is_bool($value):
                    $type = PDO::PARAM_BOOL;
                    break;
                case is_null($value):
                    $type = PDO::PARAM_NULL;
                    break;
                default:
                    $type = PDO::PARAM_STR;
                    break;
            }
        }
        $this->stmt->bindValue($param, $value, $type);
    }

    public function rowCount()
    {
        return $this->stmt->rowCount();
    }

    public function closeCursor()
    {
        $this->stmt->closeCursor();
    }
}

我希望让交易ID显示在我的文本框下方并提交按钮,或者显示异常和错误。相反,它只是文本框和提交按钮而返回。我尝试将echo语句放在以$ getTransactID = new DatabaseConnection();开头的数据库连接行之间,但是只有直接在$ getTransactID = new DatabaseConnection();下的一个语句。出现。其余的都不是。

编辑:感谢Barmar的评论,问题已解决。原来,我在页面中使用的方法名称与在我正在使用的PDO类中定义的方法名称不同。

1 个答案:

答案 0 :(得分:0)

@Barmar用他的评论之一回答了这个问题。事实证明,我已经在PDO类中命名了一个方法setQuery()(我是两年前完成的),并且在页面代码中,我尝试使用名称query()来调用该方法。