[英]PHP not displaying anything in MAMP
我對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>
這是因為(我認為)這行是不好的:
mysql_select_db(primrose);
在數據庫名稱周圍添加引號:
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
函數,因為它們是deprecatid。 改用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>
嘗試設置error_reporting(E_ALL);
到打開PHP之后。 如果沒有,請確保在您的php.ini中打開了錯誤報告功能
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.