PDO,从两个表foreach语句中获取内容

问题描述 投票:-3回答:2

这可能看起来像重复但我点击了所有的并尝试了但它不会修复它所以我认为id发布我的代码并寻求帮助!

我正在尝试将数据回显到一个表中,到目前为止唯一有效的是回复和主题,我无法弄清楚为什么其他人不会工作,如上所述,我已尝试过所有PDO帖子但没有他们已经修好了,所以请你帮忙,因为我对PDO比较新,所以希望能够看到我出错的地方。

我期待它做的是票的主题,日期,出版商以及它是否活跃,但只有回复和主题显示,所以我想知道为什么因为它让我感到困惑。

<table class="ui single line table">
<thead>
<tr>
    <th>Subject</th>
    <th>Created</th>
    <th>By</th>
    <th>Replies</th>
    <th>Status</th>
</tr>
</thead>
<tbody>
<?php

/* Ticket Info */
$stmt = $dbh->prepare("SELECT * FROM support_tickets WHERE username = :username ORDER BY id DESC");
$stmt->bindParam(':username', $userName);
$stmt->execute();
$tickets = $stmt->fetchAll(PDO::FETCH_ASSOC);

foreach ($tickets as $myticket)
{
    $ticket_id = $myticket['id'];
    $stmt = $dbh->prepare("SELECT * FROM support_messages WHERE on_ticket = :on_ticket");
    $stmt->bindParam(':on_ticket', $ticket_id);
    $stmt->execute();
    $getReplies = $stmt->fetchAll(PDO::FETCH_ASSOC);

    $replyNo = count($getReplies);
    $simpleHash = sha1($ticket_id . $companyName);

    echo '<tr>';
    echo '<td><a href="index.php?t=' . base64_encode($myticket['id']) . '&h=' . $simpleHash . '">' . $myticket['subject'] . '</a></td><td>' . $myTicket[0]['date'] . '</td><td>' . $tickets[0]['from_name'] . '</td> <td>' . $replyNo . '</td> <td>' . $myTicket['status'] . '</td>';
    echo '</tr>';
}

?>
</tbody>
</table>
php database pdo echo
2个回答
0
投票

这是我更新的解决方案。对于错误报告和异常处理,我建议您阅读thisthis

<?php
// Db configs.
define('HOST', 'localhost');
define('PORT', 3306);
define('DATABASE', 'yourDb');
define('USERNAME', 'yourDbUsername');
define('PASSWORD', 'yourDbPassword');
define('CHARSET', 'utf8');

/*
 * Error reporting. Also, define an error handler, an exception handler and, eventually, 
 * a shutdown handler function to handle the raised errors and exceptions correspondingly.
 * 
 * @link http://php.net/manual/en/function.error-reporting.php
 * @link http://php.net/manual/en/function.set-error-handler.php
 * @link http://php.net/manual/en/function.set-exception-handler.php
 * @link http://php.net/manual/en/function.register-shutdown-function.php
 */
error_reporting(E_ALL);
ini_set('display_errors', 1); // SET IT TO 0 ON A LIVE SERVER!

/*
 * Create a PDO instance as db connection to db.
 * 
 * @link http://php.net/manual/en/class.pdo.php
 * @link http://php.net/manual/en/pdo.constants.php
 * @link http://php.net/manual/en/pdo.error-handling.php
 * @link http://php.net/manual/en/pdo.connections.php
 */
$connection = new PDO(
        sprintf('mysql:host=%s;port=%s;dbname=%s;charset=%s', HOST, PORT, DATABASE, CHARSET)
        , USERNAME
        , PASSWORD
        , [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => FALSE,
    PDO::ATTR_PERSISTENT => FALSE,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        ]
);

/*
 * Read needed variables (from HTTP POST or GET, for example).
 */
$userName = 'Example User';
$companyName = 'Example Co.';


/*
 * The SQL statement to be prepared. Notice the so-called named markers.
 * They will be replaced later with the corresponding values from the
 * bindings array when using PDOStatement::bindValue.
 * 
 * When using named markers, the bindings array will be an associative
 * array, with the key names corresponding to the named markers from
 * the sql statement.
 * 
 * You can also use question mark markers. In this case, the bindings 
 * array will be an indexed array, with keys beginning from 1 (not 0).
 * Each array key corresponds to the position of the marker in the sql 
 * statement.
 * 
 * @link http://php.net/manual/en/mysqli.prepare.php
 */
$sql = 'SELECT 
            tic.*,
            (
                SELECT COUNT(*) 
                FROM support_messages 
                WHERE on_ticket = tic.id 
            ) AS numberOfReplies 
        FROM support_tickets AS tic 
        WHERE tic.username = :username 
        ORDER BY tic.id DESC';

/*
 * The bindings array, mapping the named markers from the sql
 * statement to the corresponding values. It will be directly 
 * passed as argument to the PDOStatement::execute method.
 * 
 * @link http://php.net/manual/en/pdostatement.execute.php
 */
$bindings = [
    ':username' => $userName,
];

/*
 * Prepare the sql statement for execution and return a statement object.
 * 
 * @link http://php.net/manual/en/pdo.prepare.php
 */
$statement = $connection->prepare($sql);

/*
 * Execute the prepared statement. Because the bindings array
 * is directly passed as argument, there is no need to use any
 * binding method for each sql statement's marker (like
 * PDOStatement::bindParam or PDOStatement::bindValue).
 * 
 * @link http://php.net/manual/en/pdostatement.execute.php
 */
$statement->execute($bindings);

/*
 * Fetch tickets (all at once) in an array.
 * 
 * @link http://php.net/manual/en/pdostatement.fetchall.php
 */
$tickets = $statement->fetchAll(PDO::FETCH_ASSOC);

/*
 * Close the prepared statement.
 * 
 * @link http://php.net/manual/en/pdo.connections.php Example #3 Closing a connection.
 */
$statement = NULL;

/*
 * Close the previously opened database connection.
 * 
 * @link http://php.net/manual/en/pdo.connections.php Example #3 Closing a connection.
 */
$connection = NULL;
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>STO answer</title>
    </head>
    <body>
        <table class="ui single line table">
            <thead>
                <tr>
                    <th>Subject</th>
                    <th>Created</th>
                    <th>By</th>
                    <th>Replies</th>
                    <th>Status</th>
                </tr>
            </thead>
            <tbody>
                <?php
                foreach ($tickets as $ticket) {
                    $ticketId = $ticket['id'];
                    $ticketSubject = $ticket['subject'];
                    $ticketDate = $ticket['date'];
                    $ticketPublisher = $ticket['from_name'];
                    $ticketStatus = $ticket['status'];
                    $ticketNumberOfReplies = $ticket['numberOfReplies'];

                    $hash = sha1($ticketId . $companyName);
                    $encodedTicketId = base64_encode($ticketId);

                    $url = 'index.php?t=' . $encodedTicketId . '&h=' . $hash;
                    ?>
                    <tr>
                        <td>
                            <a href="<?php echo $url; ?>">
                                <?php echo $ticketSubject; ?>
                            </a>
                        </td>
                        <td>
                            <?php echo $ticketDate; ?>
                        </td>
                        <td>
                            <?php echo $ticketPublisher; ?>
                        </td>
                        <td>
                            <?php echo $ticketNumberOfReplies; ?>
                        </td>
                        <td>
                            <?php echo $ticketStatus; ?>
                        </td>
                    </tr>
                    <?php
                }
                ?>
            </tbody>
        </table>
    </body>
</html>

-2
投票

可能有效的是参数是字符串的那些,而不是int的那些...

你能试一下吗:

$stmt->bindParam(':on_ticket', (int)$ticket_id);

甚至(所以代码更明确)

$stmt->bindParam(':on_ticket', (int)$ticket_id, PDO::PARAM_INT);
© www.soinside.com 2019 - 2024. All rights reserved.