[英]SQL Data: HTML Table display of column data containing multiple rows
I'm not even sure how to ask the question properly, otherwise I probably would have found an answer. 我什至不确定如何正确地提出问题,否则我可能会找到答案。 So, I will try to explain as best I can.
因此,我将尽力解释。
I'm working with a script called SLDB (I did not write these scripts), and as it stands, the scripts functions as advertised. 我正在使用一个名为SLDB的脚本(我没有编写这些脚本),按目前的情况来看,这些脚本的功能与广告宣传的一样。 The data that I store, I wish to call back into a website table (not what the scripts were designed to do).
我希望将存储的数据回调到网站表中(而不是脚本的设计目的)。
Currently, the way SLDB is set up, it stores fields in this fashion: 当前,SLDB的设置方式以这种方式存储字段:
ID Field Value
001 name name1
001 size size1
002 name name2
002 size size2
003 name name3
003 size size3
In the main script that passes the variables to the database.php script, it passes name and size as 'fields' and then each of their values as 'values'. 在将变量传递到database.php脚本的主脚本中,它将名称和大小传递为“字段”,然后将它们的每个值传递为“值”。
What I would like to display on the webpage is: 我想在网页上显示的是:
ID Name Size
001 name1 size1
002 name2 size2
003 name3 size3
In fact, I'd much rather rewrite the original script and database.php script to create and update the table in this fashion, but before I get into that, I would much rather find a way to accomplish my task without rewriting, if possible. 实际上,我宁愿重写原始脚本和database.php脚本以这种方式创建和更新表,但在可能之前,我宁愿找到一种无需重写即可完成任务的方法。 。
I am not proficient enough with mySQL to understand how to get the fields (Field, Value) with multiple variables to display separately in a single row. 我对MySQL不够熟练,无法理解如何获取具有多个变量的字段(Field,Value)以单独显示在一行中。 Currently, this is how it prints out:
当前,它是这样打印出来的:
http://wickednight.net/contests/photo-december/results.php http://wickednight.net/contests/photo-december/results.php
This is the simple php script I've written to display the table in html: 这是我编写的用于在html中显示表格的简单php脚本:
<?php
$con=mysqli_connect("localhost","***","***","***");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM sldb_data");
echo "<table border='1' class='db-table' align='center'>
<tr>
<th>Name</th>
<th>Size</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['field'] . "</td>";
echo "<td>" . $row['value'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
I realize, as written, this script is doing exactly as it's being asked to do. 我意识到,按照编写的要求,此脚本的功能与要求的完全一样。 What I need to accomplish, however is obvious, I think.
我认为,我需要完成的工作很明显。 Any help is greatly appreciated.
任何帮助是极大的赞赏。
This should work: 这应该工作:
SELECT t1.uuid, t1.value AS name, t2.value AS size FROM sldb_data t1, sldb_data t2
WHERE t1.uuid = t2.uuid AND t1.field = "name" AND t2.field = "size"
Short explanation: 简短说明:
You can give a different name to a table in a query. 您可以为查询中的表使用其他名称。 This is nice when your tables have long or meaningless names.
当您的表名称很长或毫无意义时,这很好。 This is done in the FROM part.
这是在FROM部分完成的。 "sldb_data t1" gives the name "t1" to the table "sldb_data".
“ sldb_data t1”为表“ sldb_data”赋予名称“ t1”。
As we want to fetch two different rows from the same table we have to use the same table twice but we must give it two different names otherwise mysql is not knowing what exactly you want. 因为我们想从同一个表中获取两个不同的行,所以我们不得不使用同一个表两次,但是必须给它两个不同的名称,否则mysql并不知道您到底想要什么。 You can think of it as if you had two identical tables.
您可以认为它好像有两个相同的表。
Now we look for a row in t1 where field="name", in t2 where field="size" but only match those together with same uuid. 现在我们在t1的field =“ name”中查找一行,在t2的field =“ size”中查找一行,但仅将它们与相同的uuid匹配。
After the SELECT we just list the fields we want in our result. SELECT之后,我们只列出我们想要的结果中的字段。 And again we give a name to the fields.
再一次,我们给字段起一个名字。 But with fields you have to use the "as".
但是对于字段,您必须使用“ as”。
I am not sure what you want but I can say that its not good idea to storing form field name and their each value. 我不确定您想要什么,但是可以说存储表单字段名称及其每个值不是一个好主意。 May be you already know we use name and value for inserting purpose with using post/get method.
可能您已经知道我们使用post / get方法将名称和值用于插入目的。
Your sldb_data table may have 您的sldb_data表可能有
id(PK) name( varchar) size(int)
in three columns and then change your query like 在三列中,然后像
SELECT name,size FROM sldb_data
and your code should 和你的代码应该
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['size'] . "</td>";
echo "</tr>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.