最近我一直在建立我的第一个网站,因为这是我第一次使用PHP和数据库,我遇到了麻烦。我已经研究了三个多小时来寻找答案。经过几个教程后,我无法获得数据库信息,以显示在我的网页上的表格内。显示的项目只有一个表格标题和一个空白行。我是否没有连接到数据库,或者是与我的代码有关的其他事情?下面是来自PHP的代码,它位于我的html文件中。
<?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是什么意思?为什么密码是空的?任何帮助都是非常感谢的。如果你需要更多关于我的数据库,我的网站主机,更多的代码,或任何其他信息,请随时询问。
发布于 2014-03-17 08:12:32
试一试
<?php
while($row = mysql_fetch_array($result)){
?>
<tr><td> <?php echo $row['name']; ?> </td><td> <?php echo $row['address']; ?> </td><td> <?php echo $row['ages']; ?> </td></tr>
<?php
}
?>localhost是托管数据库'jcsavage_initiumjobs‘的服务器的域
当您在Userid 'root‘上设置密码时,您将添加该密码,而不是空的(不存在的)密码
例如,如果您将超级用户的密码设置为'andBranch‘,那么您连接的世界代码就是
$connection = mysql_connect('localhost', 'root', 'andBranch');发布于 2014-03-17 13:13:51
试试这段代码
<?php
$connection = mysqli_connect('localhost', 'root', '','jcsavage_initiumjobs');
if(!$connection){
die("Could not connect:" . mysqli_connect_error());
}
?>
<html>
<body>
<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
$statement = "SELECT name, address, ages FROM companies ORDER BY name";
if($result = mysqli_query($connection, $statement))
{
while($data = mysqli_fetch_object($result))
{
echo "<tr><td>$data->name</td><td>$data->address</td><td>$data->ages</td></tr>";
}
}
?>
</table>
</body>
</html>https://stackoverflow.com/questions/22444853
复制相似问题