【问题标题】:MySQLi while loop returning empty array.MySQLi while 循环返回空数组。
【发布时间】:2014-05-29 11:41:10
【问题描述】:

好的,我正在制作票务系统,这是 sql 查询和 while 循环的代码:

if (mysqli_num_rows($query) === 0) {
    header('Location: /tickets');
}

$username = array();
$message = array();
$timestamp = array();
$status = array();
$subject = array();

while (var_dump($row = mysqli_fetch_assoc($query))) {
    $username[] = $row['username'];
    $message[] = $row['message'];
    $timestamp[] = $row['timestamp'];
    $status[] = $row['status'];
    $subject[] = $row['subject'];
}


if (isset($_POST['submit'])) {
    $new_message = $_POST['message'];

    $new_message = mysqli_real_escape_string($con, $new_message);

    mysqli_query($con, "INSERT INTO tickets VALUES(
    NULL,
    '".$id."',
    '".$username."',
    '".$subject."',
    '1',
    '".date('H M H:i')."',
    '".$new_message."'
    )");
}

?>

<h2>Unique Ticket ID: <?php echo $id; ?></h2>

<?php

foreach ($message as $msg) {
    echo '
    <div class="ticket-message clearfix">
        <h4>'.$username.' ['.$timestamp.']</h4>
        <p>'.$msg.'</p>
    </div>';
}

如果我尝试排列打印或回显这些结果中的任何一个,它会打印“Array”而不是其他任何内容。我有一个 var_dump,结果如下:

array(7) { ["id"]=> string(2) "28" 
           ["ticket_id"]=> string(10) "TICK_71323" 
           ["username"]=> string(6) "Jordan" 
           ["subject"]=> string(14) "ticket subject" 
           ["status"]=> string(1) "1" 
           ["timestamp"]=> string(13) "Thu May 12:34" 
           ["message"]=> string(16) "ticket message " 
        }

显然数据是要提取的吗?为什么我无法正确获取值?

【问题讨论】:

  • exit 在您的header() 之后

标签: php mysql arrays mysqli


【解决方案1】:

$username、$message 等都是数组,所以你需要使用索引来访问它们,试试这个:

if (mysqli_num_rows($query) === 0) {
    header('Location: /tickets');
}

$username = array();
$message = array();
$timestamp = array();
$status = array();
$subject = array();

while ($row = mysqli_fetch_assoc($query)) {
    $username[] = $row['username'];
    $message[] = $row['message'];
    $timestamp[] = $row['timestamp'];
    $status[] = $row['status'];
    $subject[] = $row['subject'];
}


if (isset($_POST['submit'])) {
    $new_message = $_POST['message'];

    $new_message = mysqli_real_escape_string($con, $new_message);
for($i=0;$i<count($username);$i++)
{
    mysqli_query($con, "INSERT INTO tickets VALUES(
    NULL,
    '".$id."',
    '".$username[$i]."',
    '".$subject[$i]."',
    '1',
    '".date('H M H:i')."',
    '".$new_message."'
    )");
}
}

这将为每个用户名插入一张票...而且 $id 变量不知道它来自哪里...也许必须在 while bucle 中检索。另一种方法:

if (mysqli_num_rows($query) === 0) {
    header('Location: /tickets');
}

$username = "";
$message = "";
$timestamp = "";
$status = "";
$subject = "";

if($row = mysqli_fetch_assoc($query)) {
    $username = $row['username'];
    $message = $row['message'];
    $timestamp = $row['timestamp'];
    $status = $row['status'];
    $subject = $row['subject'];
}


if (isset($_POST['submit'])) {
    $new_message = $_POST['message'];

    $new_message = mysqli_real_escape_string($con, $new_message);

    mysqli_query($con, "INSERT INTO tickets VALUES(
    NULL,
    '".$id."',
    '".$username."',
    '".$subject."',
    '1',
    '".date('H M H:i')."',
    '".$new_message."'
    )");
}

如果 $query 记录集中只有一行。

【讨论】:

  • $id 变量来自文档中较早的 GET 请求。它定义了将这些记录组合在一起的工单 ID
  • 然后尝试发布的代码...我想您需要第二种方法。
猜你喜欢
  • 2014-08-11
  • 2019-06-07
  • 2012-12-10
  • 1970-01-01
  • 2013-12-09
  • 2022-11-30
  • 2015-11-17
  • 1970-01-01
相关资源
最近更新 更多