我知道这个问题在 4 年前就被问过了,但由于没有答案被标记为正确,我想我可能会加入。
首先,让我们从升级mysql
和使用mysqli
——我个人最喜欢的,你也可以使用PDO
。您是否尝试过使用$_GET
拉取您想查看的任何产品的 id,然后将它们全部一起显示或一次显示一个?
它可能看起来像这样:
<?php // start by creating $mysqli connection
$host = "localhost";
$user = "username";
$pass = "password";
$db_name = "database";
$mysqli = new mysqli($host, $user, $pass, $db_name);
if($mysqli->connect_error)
{
die("Having some trouble pulling data");
exit();
}
假设连接成功,我们继续检查是否设置了 ID。在这种情况下,我通过假定为id
. 你可以让它更复杂,或者在这里采取不同的方法。
if(isset($_GET['id']))
{
$id = htmlentities($_GET['id']);
$query = "SELECT * FROM table WHERE id = " . $id;
}
else
{
// if no ID is set, just bring all the results down
// then you can modify how, and which table the results
// are being used.
$query = "SELECT * FROM table ORDER BY id"; // the query can be changed to whatever you would be prefer
}
一旦我们决定了一个查询,我们就会开始查询数据库以获取信息。我有三个步骤:
检查查询 > 检查表中的记录 > 遍历角色并为每个角色创建一个对象。
if($result = $mysqli->query($query))
{
if($result->num_rows > 0)
{
while ($row = $result->fetch_object())
{
// you can set up your element here
// you can set it up in whatever way you want
// to see your product being displayed, by simply
// using $row->column_name
// each column becomes an object here. So your id
// column would be pulled using $row->id
echo "<h1>" . $row->name . "</h1>";
echo "<p>" . $row->description . "</p>";
echo "<img src=" . $row->image_path . ">";
// etc ...
}
}
else
{
// if no records match the selected ID
echo "Nothing to see here...";
}
}
else
{
// if there's a problem with the query
echo "A slight problem with your query.";
}
$mysqli->close(); // close connection for safety
?>
我希望这能回答你的问题,如果你仍然被这个问题困扰,可以帮助你。这是你可以用 MySQLi 和 PHP 做的事情的基本框架,你总是可以使用一些 Ajax 来使页面更具交互性和用户友好性。