如何从 MySQL 数据库中获取特定数据到我的 PHP 表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16152067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:37:10  来源:igfitidea点击:

How to fetch specific data from MySQL database to my PHP table?

phpmysqlsql

提问by strellson

I want to fetch data from MySQL database to my table in .php file. In every row of my table I want to display title, text and attachment($name) that user can download. The problem is when I display that, I get all attachments from database shown in list in every table row in my .php file. So I want only to display only one attachment per row, from database, that have same ID as title and text of database table row.

我想从 MySQL 数据库中获取数据到我在 .php 文件中的表。在表格的每一行中,我想显示用户可以下载的标题、文本和附件($name)。问题是当我显示它时,我从 .php 文件中每个表行的列表中显示的数据库中获取所有附件。所以我只想每行只显示一个附件,来自数据库,与数据库表行的标题和文本具有相同的 ID。

This is my database table:

这是我的数据库表:

CREATE TABLE IF NOT EXISTS `table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(30) NOT NULL,
`text` varchar(30) NOT NULL,
`name` varchar(30) NOT NULL,
`type` varchar(30) NOT NULL,
`size` int(11) NOT NULL,
`content` longblob NOT NULL,
PRIMARY KEY (`id`)  )

My code:

我的代码:

<?php
include('config.php');
$sqlget="SELECT * FROM table ORDER BY timestamp DESC";
$sqldata= mysqli_query($dbcon, $sqlget) or die ('error');
echo"<table>";
while ($row=mysqli_fetch_array($sqldata,
MYSQLI_ASSOC)) {
echo "<tr><td>";
echo"<b><font color='#DF01A5'> Title: ".$row['title']."</font></b>";
echo "<br/>";
echo $row['text'];
echo "<br/>";
echo "<b><font color='#DF01A5'>Attachment: </font>";
?>
<?php
$con = mysql_connect('localhost', 'root', 'pass') or die(mysql_error());
$db = mysql_select_db('database', $con);
$query = "SELECT  id, name FROM table";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="download.php?id=<?php echo urlencode($id);?>"
><?php echo urlencode($name);?></a> <br>
<?php 
}
}
mysql_close();
echo"</table";
?>

回答by moskito-x

Use only one while loop

只使用一个 while 循环

$sqlget="SELECT * FROM table ORDER BY timestamp DESC";
$sqldata= mysqli_query($dbcon, $sqlget) or die ('error');
echo"<table>";
while ($row=mysqli_fetch_array($sqldata,MYSQLI_ASSOC)) {
   echo "<tr><td>";
   echo"<b><font color='#DF01A5'> Title: ".$row['title']."</font></b>";
   echo "<br/>";
   echo $row['text'];
   echo "<br/>";
   echo "<b><font color='#DF01A5'>Attachment: </font>";
   echo "<a href=\"download.php?id=".$row['id']."\">".$row['name']."</a><br />\n";
   ...
}
echo '</table>';

回答by Unexpected Pair of Colons

Assuming your two tables are related on the ID alone:

假设您的两个表仅与 ID 相关:

$sql = $db_conn->query("SELECT t1.title, t1.text, t2.ID AS AID, t2.name FROM table1 AS t1 LEFT JOIN table2 AS t2 ON t1.ID = t2.ID ORDER BY t1.timestamp");

echo '<table><thead><tr><th>Title</th><th>Description</th><th>Attachment</th></tr></thead>';
while($row = $sql->fetch_array())
{
    echo '<tr>';
    echo '<td>' . $row['title'] . '</td>';
    echo '<td>' . $row['text'] . '</td>';
    echo '<td><a href="download.php?id=' . urlencode($row['AID']) . '">' . $row['name'] . '</a></td>';
    echo '</tr>';
}
echo '</table>';

Not tested, but start with this.

未测试,但从这里开始。

回答by Andrew Therin

We'll need a sample output from this table. I'm still not sure how those attachments are being stored. In the meantime, this PHP function should return only one row: http://www.php.net/manual/en/mysqli-result.fetch-row.php

我们需要这个表的样本输出。我仍然不确定这些附件是如何存储的。同时,这个 PHP 函数应该只返回一行:http: //www.php.net/manual/en/mysqli-result.fetch-row.php

Edit: unnecessary comment.

编辑:不必要的评论。

回答by Subedi Kishor

For your particular condition, this will help help:

对于您的特定情况,这将有助于:

$sqlget = "SELECT `title`, `text`, `name` FROM `table` WHERE `title` = `text`";

Enjoy...

享受...