[英]Viewing all of table with php and mysql
So, I have pretty limited knowledge when it comes to mysql. 因此,关于mysql我的知识非常有限。 Usually when dealing with content on our site, I'm used to loading it from expressionengine, our cms, which I know fine.
通常,在处理我们网站上的内容时,我习惯于从ExpressionEngine(我们的cms)加载内容,我知道这很好。 But recently I have to update this page that loads its data directly from our database.
但是最近我不得不更新此页面,以直接从我们的数据库中加载其数据。 I took a course on mysql a while back, but I still don't really know how to use it.
我前一段时间上过mysql课程,但我仍然不太了解如何使用它。
The page doesn't need to use too much information, its basically just a list of awards presented to different businesses. 该页面不需要使用太多信息,它基本上只是提供给不同企业的奖项列表。 I assume the table on the database just has a column for Business name, City, Year, and Type of award won.
我假设数据库中的表只有一列,其中包含“企业名称”,“城市”,“年份”和“获奖类型”。 Stuff like that.
像那样的东西。
Now accessing the database doesn't seem like it would be too hard, and I feel like I could google around and find what I need. 现在访问数据库似乎并不难,而且我觉得自己可以四处搜寻并找到所需的东西。 But to get started, I would just like to see the actual table!
但是要开始使用,我只想看看实际的表! Why is it so complicated of a procedure to do?
为什么程序这么复杂?
The code on the file that I'm working on was set up like this to access the database: 我正在处理的文件上的代码是这样设置的,以访问数据库:
$user_name = "xxxxxxxxxx";
$password = "xxxxxxxxxx";
$server = "xxxxxxxxxx";
$database = "xxxxxxxxxx";
$conni = new mysqli($server, $user_name, $password, $database);
if (mysqli_connect_errno()) {
printf("<center>
<strong>No connection to database possible!<br />
Please contact us!</strong>
</center>");
exit();
}
I figured out how to view all tables on the database, by doing this: 通过这样做,我弄清楚了如何查看数据库中的所有表:
$result = mysql_query("show tables");
while($table = mysql_fetch_array($result)) {
echo($table[0] . "<BR>");
}
So I know the name of the table I'm dealing with. 所以我知道我要处理的表的名称。 I found a way to see all the column names:
我找到了一种查看所有列名称的方法:
$query = "select * from my_tablename";
$result = mysql_query($query);
$numcolumn = mysql_num_fields($result);
for ( $i = 0; $i < $numcolumn; $i++ ) {
$columnnames = mysql_field_name($result, $i);
echo $columnnames . "<br>";
}
But I still can't find how to just display the table itself. 但是我仍然找不到如何仅显示表格本身。 I might be thinking about this wrong, or not have the full idea, but what I want is a simple way to display the entire table, like with an html table.
我可能正在考虑这个错误,或者没有完整的想法,但是我想要的是一种简单的方法来显示整个表,例如html表。 How can that be done?
那怎么办?
You can build the <thead>
of a table with the column names you have above, however to display the results in a table format, use the mysql_fetch_row
function on the $result
of SELECT * FROM my_tablename
. 您可以使用上面的列名称构建表的
<thead>
,但是要以表格式显示结果,请对SELECT * FROM my_tablename
的$result
使用mysql_fetch_row
函数。 To reuse the $result
after you've got the field names as in your last block, use mysql_data_seek
: 要像上一个块中那样获得字段名称后重用
$result
,请使用mysql_data_seek
:
mysql_data_seek($result, 0);
And then you can iterate through your results to get the contents of the table: 然后,您可以遍历结果以获取表的内容:
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
foreach ($row as $value) {
echo '<td>' . $value . '</td>';
}
echo '</tr>';
}
mysql_fetch_row
will get a numerically indexed array with where each value is the column value in MySQL, therefore by running foreach you get the value of each column. mysql_fetch_row
将获得一个数值索引的数组,其中每个值都是MySQL中的列值,因此,通过运行foreach可以获取每个列的值。 Put it inside of a <td>
, wrap it all in a <tr>
and you have yourself a table! 将其放在
<td>
,将其全部包裹在<tr>
,您便拥有了一张桌子!
Just for good measure, you should switch to using mysqli
in PHP as the mysql_
functions are deprecated in later versions. 出于良好的考虑,您应该切换到在PHP中使用
mysqli
,因为在更高版本中不建议使用mysql_
函数。 To switch over, use the PHP documentation to search for the mysql_
functions you're currently using, there will be a pink message which will direct you to the mysqli
equivalents 要进行切换,请使用PHP文档搜索您当前正在使用的
mysql_
函数,系统将显示一条粉红色消息,将您定向到mysqli
等效项
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.