dongqixian8474 2016-11-22 22:41
浏览 16
已采纳

不从多个表返回数据(php / sql)

I am trying to return data from shoes table and types table. I have done an inner join on type_id. It is only returning data from the types table, I think it may have something to do with my if statement ?

Any help would be appreciated.

<!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;
?>
  • 写回答

2条回答 默认 最新

  • dongzhoulong1797 2016-11-22 22:47
    关注

    You're calling $stmt->fetch() twice. The first one returns the first row of results, the second one returns the second row of results. But your query only returns one row, so the second if fails and doesn't print anyting.

    Call $stmt->fetch() once, and use the variable to print all the information.

    if ($row = $stmt->fetch()) {
        echo "<h2>".$row['typeOfShoe']."</h2>";
        echo "<h2>".$row['name']."</h2>";
        echo "<p>".$row['description']."</p>";
        echo "<p>".$row['price']."</p>";  
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划