【问题标题】:Using 3 Variables for a multitude of data对大量数据使用 3 个变量
【发布时间】:2017-10-19 04:18:12
【问题描述】:

(标题有点奇怪,随便改,我不知道该放什么) HTML 表将是 MySQL 表中最后 5 件事的历史记录,当加载第一行时,它将存储在 HTML 表中第一行的变量中,然后加载/获取下一个 MYSQL 行,然后然后将 HTML 表中的第二行更改为 MYSQL 表的第二行。

例子:

for each row:
$username = row["username"]
$name = row["name"]

最终结果应该在 HTML 表格中: 这些是使用上面的变量,我需要根据 MySQL 表中的最后 5 个表,它们都是不同的数据

用户名 一个 Name1(MySQL 第 1 行) 两个 Name2(MySQL 第 2 行) 三个 Name3(MySQL 第 3 行) 四名4(MySQL第4行)

到目前为止的代码:

$stmt = $dbConnection->prepare("SELECT * FROM history ORDER BY id DESC LIMIT 5");
$stmt->execute();



foreach ($stmt as $row) {
    $username = $row['username'];
    $gamemode = $row['gamemode'];
    $winnings = $row['won'];
}

【问题讨论】:

  • 为什么需要变量? $row 包含您需要的数据,因此请使用它。
  • 例如,该表将是mysql表中最后5件事的历史记录,当第一行加载时,它存储在HTML表中第一行的变量中,然后加载/获取下一个 MYSQL 行,然后将 HTML 表中的第二行更改为 MYSQL 表的第二行。
  • 你的问题很不清楚。我建议,您提供最后 5 个表格行(带有字段名称和字段值),然后参考字段名称和字段值,以便更好地描述您的问题。
  • 更新了问题,应该会有所帮助。
  • 行不会“改变”。通常,您循环遍历数据并发出具有正确值的表的一行。完整的 HTML 响应应该以长格式指定所有内容。变量是没有意义的,除非你将它们用于某事。

标签: php mysql pdo


【解决方案1】:

您可以使用PDOStatement::fetchAll 方法来实现。我建议您避免从 php.ini 构建 html 代码(如 html 表)。随便问什么。

<?php
// Create a PDO instance as db connection to a MySQL db.
$dbConnection = new PDO(
        'mysql:host=localhost;port=3306;dbname=demo;charset=utf8'
        , 'user'
        , 'pass'
        , array(
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_EMULATE_PREPARES => FALSE,
    PDO::ATTR_PERSISTENT => TRUE
        )
);

$sql = 'SELECT * FROM history ORDER BY id DESC LIMIT 5';

$stmt = $dbConnection->prepare($sql);
$executed = $stmt->execute();

/*
 * Fetch the result set in form of an array.
 * It's used later to build the html table.
 * 
 *  Array
 *  (
 *      [0] => Array
 *          (
 *              [username] => one
 *              [name] => name1
 *          )
 *  
 *      [1] => Array
 *          (
 *              [username] => two
 *              [name] => name2
 *          )
 *  
 *      [2] => Array
 *          (
 *              [username] => three
 *              [name] => name3
 *          )
 *      ...
 *      [4] => Array
 *          (
 *              [username] => five
 *              [name] => name5
 *          )
 *  )
 */
$resultset = $stmt->fetchAll(PDO::FETCH_ASSOC);

// Just for testing. Delete this in the end.
echo '<pre>' . print_r($resultset, TRUE) . '</pre>';
?>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />

        <title>Demo</title>
    </head>
    <body>

        <table>
            <tr>
                <td>Username</td>
                <td>Name</td>
            </tr>
            <?php
            // Loop through the resultset array and build each html row.
            foreach ($resultset as $row) {
                $username = $row['username'];
                $name = $row['name'];
                ?>
                <tr>
                    <td>
                        <?php echo $username; ?>
                    </td>
                    <td>
                        <?php echo $name; ?>
                    </td>
                </tr>
                <?php
            }
            ?>
        </table>
    </body>
</html>

【讨论】:

    【解决方案2】:

    为了使代码尽可能接近,您可以使用数组来存储数据,如下所示:

    $stmt = $dbConnection->prepare("SELECT * FROM history ORDER BY id DESC LIMIT 5");
    $stmt->execute();
    
    $usernames = array();
    $gamemodes = array();
    $winnings = array();
    
    foreach ($stmt as $row) {
        $usernames[] = $row['username'];
        $gamemodes[] = $row['gamemode'];
        $winnings[] = $row['won'];
    }
    

    然后,您可以像这样访问它们:

    echo $usernames[1]; // Second name
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-28
      • 1970-01-01
      • 2011-10-05
      • 2022-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多