duanlan5320 2016-11-16 23:57
浏览 47
已采纳

如何将这个select sql语句更改为另一个select sql?

I have code the below code works fine but i want to have one sql statement instead of few in this code so when i try to change into one i get an error.

     $eventID = $_GET['id'];

    $sql = "SELECT * FROM te_events where eventID='$eventID'";
    $result = $conn->query($sql);

    while($row = $result->fetch_assoc()) 
    {
      $eventTitle = $row['eventTitle'];
      $eventDescription = $row['eventDescription'];
      $eventStartDate = $row['eventStartDate'];
      $eventEndDate = $row['eventEndDate'];
      $eventPrice = $row['eventPrice'];
      $venueID = $row['venueID'];
      $catID = $row['catID'];

      $sql2 = "SELECT * FROM te_venue where venueID='$venueID'";
      $result2 = $conn->query($sql2);

      while($row2 = $result2->fetch_assoc()) 
      {
        $venueName = $row2['venueName'];
      }
      $sql3 = "SELECT * FROM te_category where catID='$catID'";
      $result3 = $conn->query($sql3);

      while($row3 = $result3->fetch_assoc()) 
      {
        $catName = $row3['catDesc'];
      }
    }
  ?>

I changed the code into this but it seems it is not working.

 <?php
 $eventID = $_GET['id'];

    $sql = "SELECT * FROM te_events where eventID='$eventID' AND where venueID='$venueID' From te_venue AND where catID='$catID' From te_category";
     $queryresult = mysqli_query($conn, $sql) or die(mysqli_error($conn));
    while ($row = mysqli_fetch_array($queryresult)) {
      $eventTitle = $row['eventTitle'];
      $eventDescription = $row['eventDescription'];
      $eventStartDate = $row['eventStartDate'];
      $eventEndDate = $row['eventEndDate'];
      $eventPrice = $row['eventPrice'];
      $venueID = $row['venueID'];
      $catID = $row['catID'];
      $catName = $row['catDesc'];
      $venueName = $row['venueName'];



    }

  ?>

And i get this error.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where venueID='' From te_venue AND where catID='' From te_category' at line 1

  • 写回答

2条回答 默认 最新

  • dousuiben8395 2016-11-17 00:14
    关注

    Yes, you can do that. But in order to get the fields that you want from all three tables, you have to join them together.

    Look at this article on W3S: http://www.w3schools.com/sql/sql_join.asp. It explains SQL JOIN syntax and the basic theory behind.

    If you just joined venue and event Your select statement looks like:

    SELECT * FROM te_event 
    JOIN te_venue 
    ON te_vendue.venueID = te_event.venueID 
    WHERE te_event.eventID = $eventID
    

    The category table is similar.

    Note: In general, use of SELECT * is discouraged. Your should list the fields that you want returned from the tables. ie. SELECT te_eventID, te_venueID

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?