gpt4 book ai didi

PHP 在 MAMP 中不显示任何内容

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:00 24 4
gpt4 key购买 nike

我是 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 函数,因为它们已被弃用。请改用 mysqliPDO

注意 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>

关于PHP 在 MAMP 中不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27019722/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com