Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将mysql结果行存储为包含表格式字符串的变量?_Mysql_Loops_Mysqli_While Loop - Fatal编程技术网

如何将mysql结果行存储为包含表格式字符串的变量?

如何将mysql结果行存储为包含表格式字符串的变量?,mysql,loops,mysqli,while-loop,Mysql,Loops,Mysqli,While Loop,我的目标是显示一个事务中的mysql数据,以便在不同的时间在不同的页面上查看。在事务完成后,mysql数据需要从该表中删除,以清空购物车。因此,我尝试将mysql结果存储在一个变量中,然后对其进行编码,并将其数据插入到我的Orders表中的blob格式列中。然后,我将一个具有blob值的表单输入添加到我的订单页面,以查看以前的事务。它显示表格和一行结果。while循环似乎没有将每次获取结果的迭代存储到表中,因此我的订单页面上的输出只有一个行项目,而不是它应该具有的倍数。有人能帮助我了解如何修复循

我的目标是显示一个事务中的mysql数据,以便在不同的时间在不同的页面上查看。在事务完成后,mysql数据需要从该表中删除,以清空购物车。因此,我尝试将mysql结果存储在一个变量中,然后对其进行编码,并将其数据插入到我的Orders表中的blob格式列中。然后,我将一个具有blob值的表单输入添加到我的订单页面,以查看以前的事务。它显示表格和一行结果。while循环似乎没有将每次获取结果的迭代存储到表中,因此我的订单页面上的输出只有一个行项目,而不是它应该具有的倍数。有人能帮助我了解如何修复循环,使其正确存储数据吗

我的while循环,并尝试将其连接回一个字符串,用作“我的订单”页面上的html表:

...
$loop= "";
while($row = $result->fetch_assoc()) { 
$loop .=   "<tr><td>$row[product_id]</td>
                <td>$row[price]</td>
                <td>$row[quantity]</td></tr>";
}
$products= "<table><tr><th>Product</th>
                <th>Price</th>
                <th>Quantity</th>";

 $end =          "</table>";

$products= $products.$loop.$end;
$products=base64_encode(serialize($products));
$sql = "INSERT INTO orders (date,username,response,products)
VALUES ('$time', '$username', '$serial','$products')";
if ($conn->query($sql) === TRUE) {

} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
我的订单页面:

echo "<table id= 'orders' style='text-align: center'>
<tr><th> date </th>
    <th> username  </th>
    <th> address  </th>
    <th> product total  </th>
    <th> shipping total  </th>
    <th> reference # </th>
    <th> products  </th>
    <th> packaging  </th></tr>";

$sql = "SELECT * FROM orders";
$result = $conn->query($sql);
if (!$result) {
trigger_error('Invalid query: ' . $conn->error);
}
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
    $response = $row['response'];

echo "<tr>  <td> $row[date]</td>
        <td> $row[username]</td>
        <td> $row[address]</td>
        <td> $row[prodtotal]</td>
        <td> shipping total </td>
        <td> reference #</td>
        <td> <form action= 
'http://localhost/WIP/secure_login/cart/Backend/orderview.php' method='POST'>
        <input type='hidden' name='products' value='$row[products]'>
        <input type= 'submit' value='Products'></form></td>
        <td> <form action= 
'http://localhost/WIP/secure_login/cart/Backend/packageview.php' 
method='POST'>
        <input type='hidden' name='pack' value='$row[response]'>
        <input type= 'submit' value='Packaging'></form></td></tr>";

 }}
Solarflare回答: $result->fetch\u assoc是否是您第二次使用该结果集的时候?然后检查这个。但一般来说,以这种方式存储数据并不是一个好主意。如果要存储购物车历史记录,请将其存储在购物车历史记录表中!例如,带有产品标识、价格、数量和订单表主键的表。假设您决定将价格*数量显示为历史记录中的第四列。或者,如果您希望将数据用于显示表格以外的任何其他用途,例如每月购物车产品数量的报告一小时前的日光浴


这个答案告诉我,我的第二次结果获取是没有必要的,它可以在第一次完成。它不仅解决了我的问题,而且让我意识到使用一个额外的表而不是显示保存的html代码将使我的购物车历史数据库在将来更加灵活。

是否$result->fetch\u assoc是您使用该结果集的结束时间?然后检查。但一般来说,以这种方式存储数据并不是一个好主意。如果要存储购物车历史记录,请将其存储在购物车历史记录表中!例如,带有产品标识、价格、数量和订单表主键的表。假设您决定将价格*数量显示为历史记录中的第四列。或者,如果您想将数据用于显示表格以外的任何其他用途,例如每月购物车产品数量的报告。非常好的建议。非常感谢。我没想到。我被迷住了,没有想清楚。我想,当我发现你可以保存HTML代码并且只需点击一个按钮就可以轻松地显示出来时,我感到很兴奋。换一张桌子是正确的做法。