我正在尝试从鞋表和类型表返回数据。我在type_id上做了一个内连接。它只返回类型表中的数据,我想它可能与我的if语句有关?
任何帮助都将不胜感激。
<!DOCTYPE HTML>
<html>
<head>
<title>Shoe Details</title>
<h1>Shoe Details</h1>
<a href="index.php">Search Engine</a>
<br> <br/>
<a href="design.php">Database Design and Implementation</a>
<br> <br/>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>
<body>
<?php
try{
$conn = new PDO(
'mysql:host=localhost;dbname=u1553758',
'u1553758',
'19apr96'
);
} catch (PDOException $exception) {
echo "Oh no, there was a problem" . $exception->getMessage();
}
$shoesID=$_GET['id'];
$stmt = $conn->prepare("SELECT shoes.name, shoes.description, shoes.price, types.typeOfShoe FROM shoes INNER JOIN types on types.types_id = shoes.type_id WHERE shoes.id = :id");
$stmt->bindValue(':id',$shoesID);
$stmt->execute();
if ($types=$stmt->fetch()){
echo "<h2>".$types['typeOfShoe']."</h2>";
}
else { if ($shoes=$stmt->fetch()){
echo "<h2>".$shoes['name']."</h2>";
echo "<p>".$shoes['description']."</p>";
echo "<p>".$shoes['price']."</p>";
}
}
$conn=NULL;
?>
答案 0 :(得分:0)
$stmt->fetch()
一次(假设您需要一行数据)。返回的数组将包含两个表中的所有数据。
if ($shoedata=$stmt->fetch()){
echo "<h2>".$shoedata['typeOfShoe']."</h2>";
echo "<h2>".$shoedata['name']."</h2>";
echo "<p>".$shoedata['description']."</p>";
echo "<p>".$shoedata['price']."</p>";
}
答案 1 :(得分:0)
您正在拨打$stmt->fetch()
两次。第一行返回第一行结果,第二行返回第二行结果。但是您的查询只返回一行,因此第二行if
失败并且不打印任何内容。
调用$stmt->fetch()
一次,然后使用该变量打印所有信息。
if ($row = $stmt->fetch()) {
echo "<h2>".$row['typeOfShoe']."</h2>";
echo "<h2>".$row['name']."</h2>";
echo "<p>".$row['description']."</p>";
echo "<p>".$row['price']."</p>";
}