我是PHP的新手,正在尝试使用MAMP服务器环境在php页面上显示记录信息。
属性是productID,productName,productDescription和productPrice。
我已经能够得到真正基本的PHP运行良好但每次我打开这个php文件,什么都没有显示,我正在徘徊,如果它可能与我放置的PHP文件的位置。它目前在htdocs。我将不胜感激。
由于
<?php
//connection to db
mysql_connect('localhost', 'root', 'root');
//choosing db
mysql_select_db(primrose);
$sql= "SELECT * FROM products";
$records mysql_query(sql);
?>
<html>
<head>
<title>Product Data</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
//loops over each record and for each a new record is set into the variable products
while(products=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$products['productName']."</td>";
echo "<td>".$products['productDescription']."</td>";
echo "</tr>";
} //end while
?>
</table>
</body>
</html>
答案 0 :(得分:2)
这是因为(我认为)这条线很糟糕:
mysql_select_db(primrose);
在db:
的名称周围添加引号mysql_select_db("primrose");
这一行:
$records mysql_query(sql);
更改为
$records = mysql_query($sql);
和此:
while(products=mysql_fetch_assoc($records)){
到
while($products=mysql_fetch_assoc($records)){
注1:
mysql
函数,因为它们已被删除。请改用mysqli
或PDO
。注2:
让我们使用PHP文件顶部的这两行打开错误报告:
error_reporting(E_ALL);
ini_set('display_errors', 1);
你有很多语法错误。我们使用IDE来识别它们。
所以你的最终代码是这样的:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//connection to db
mysql_connect('localhost', 'root', 'root');
//choosing db
mysql_select_db("primrose");
$sql= "SELECT * FROM products";
$records = mysql_query($sql);
?>
<html>
<head>
<title>Product Data</title>
</head>
<body>
<table width="600" border="1" cellpadding="1" cellspacing="1">
<tr>
<th>Name</th>
<th>Description</th>
</tr>
<?php
//loops over each record and for each a new record is set into the variable products
while($products=mysql_fetch_assoc($records)){
echo "<tr>";
echo "<td>".$products['productName']."</td>";
echo "<td>".$products['productDescription']."</td>";
echo "</tr>";
} //end while
?>
</table>
</body>
</html>
答案 1 :(得分:1)
尝试将error_reporting(E_ALL);
设置为on to open'ed PHP。
如果没有,请确保在php.ini