简体   繁体   English

将数据从SQL数据库存储到PHP表中时出错

[英]Error when storing data from an SQL database into a PHP table

I am learning PHP and SQL, and I am currently trying to output data from a preexisting SQL database table into a table created via PHP. 我正在学习PHP和SQL,目前正在尝试将数据从预先存在的SQL数据库表输出到通过PHP创建的表中。 As of now, I am just trying to get the data from a single column and output the data into a column in my PHP table. 到目前为止,我只是试图从单个列中获取数据并将数据输出到PHP表中的列中。 I am able to correctly gather the data one row at a time using the code: 我能够使用代码一次正确地收集一行数据:

"SELECT column FROM table WHERE row1='variable1' AND row2='variable2'"

with WHERE pinpointing which row should be outputted within column . WHERE精确指出应该在column内输出哪一行。

However, I am having issues with storing the data to output the proper result. 但是,我在存储数据以输出正确结果时遇到问题。 I am currently using the code below to output the row with the data from the SQL table: 我目前正在使用下面的代码从SQL表中输出包含数据的行:

if($con->query("SELECT column FROM table WHERE row1='variable1' AND row2='variable2'")) {
    if($output= $con->use_result()) {
        while($row = $output->fetch_assoc()) {
            echo "<td>".$output."</td>";
    }
}}

However, when I run the code, it merely blanks the row where I should be outputting the data. 但是,当我运行代码时,它只是使我应该在其中输出数据的行空白。 Could anyone please point me in the right direction to properly store and output the row into a table? 有人可以指出正确的方向,以正确地将行存储并输出到表中吗? It would be much appreciated. 将不胜感激。 Let me know if anymore of my code or other information is needed. 让我知道是否需要我的代码或其他信息。

EDIT: I should mention that the table is being generated via an HTML button. 编辑:我应该提到该表是通过HTML按钮生成的。

Please try again.. 请再试一次..

if($con->query("SELECT column FROM table WHERE row1='variable1' AND row2='variable2'")) {
    if($output= $con->use_result()) {
        while($row = $output->fetch_assoc()) {
            echo "<td>".$row['row1']."</td>";
            echo "<td>".$row['row2']."</td>";
    }
}}

I tried the following code and it worked for me. 我尝试了以下代码,它对我有用。 This has also been suggested in the comments above: 上面的评论中也建议这样做:

if($con->query("SELECT column FROM table WHERE row1='variable1' AND row2='variable2'")) {
    if($output= $con->use_result()) {
        while($row = $output->fetch_assoc()) {
            echo "<td>".$row['column']."</td>";
        }
    }
}

I was actually able to fix my code, with some help from the code used here. 在这里使用的代码的一些帮助下,我实际上能够修复我的代码。 The primary issue was that I used: 主要问题是我使用了:

if($con->query("SELECT column FROM table WHERE row1='variable1' AND row2='variable2'"))

I was able to fix my code by using: 我可以使用以下方法修复代码:

$sql = "SELECT column FROM table WHERE row1='variable1' AND row2='variable2'"; $output = $con->query($sql);

before the if statement. 在if语句之前。 I appreciate the help! 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM