【发布时间】:2014-04-22 02:19:18
【问题描述】:
最近我一直在构建我的第一个网站,因为这是我第一次使用 PHP 和数据库,所以我遇到了麻烦。我已经研究了三个多小时的答案。在学习了几个教程之后,我无法在我的网页上的表格中显示数据库信息。唯一出现的项目是一个表头和一个空白行。我没有连接到数据库还是与我的代码有关?下面是我的 html 文件中的 PHP 代码。
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('jcsavage_initiumjobs');
if(!$conmnection){
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT name, address, ages FROM companies ORDER BY name");
?>
<table id='company_tables' border='1' align='centre'>
<tr> <th colspan='3'>Companies</th> </tr>
<tr> <th>Name of Company</th> <th>Location</th> <th>Ages Accepted</th> </tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr><td> <?php . $row['name'] . ?> </td><td> <?php . $row['address'] . ?> </td><td> <?php . $row['ages'] . ?> </td></tr>
<?php
}
?>
</table>
<?php
mysql_close($connection);
?>
我还有第二个问题。字符串 localhost 和 root 是什么意思?为什么密码是空白的?任何帮助表示赞赏。如果您需要有关我的数据库、我的网站托管商、更多代码或其他任何信息的更多信息,请随时提出。
【问题讨论】:
-
调试
$result = mysql_query("SELECT name, address, ages FROM companies ORDER BY name") or die(mysql_error()); -
if 语句中的
!$connection拼写错误。 localhost 也是数据库的地址,root 是您连接的用户名,如果该用户没有密码,则密码为空。mysql_connect() -
我建议您查看mysqli php object。这是一种更直观的连接 mysql 数据库的方法。
-
第二个问题:简而言之,localhost 是您的主机名,您使用的主机只能在您的 PC 上运行。 root 是用户名,例如:谁使用 localhost/phpmyadmin。并且密码是空白的,因为你没有设置它。
标签: php html database html-table