左连接多个表

时间:2013-09-10 05:15:30

标签: php mysql

我正在尝试执行以下MySQL查询:

SELECT event.event_id, venue.venue_name, venue.alt_venue_name, venue.venue_address1, venue.cross_street, dictionary_city.city_name, venue.zip, dictionary_filter.filter_name, venue.capacity, event.start_date, event.end_date, dictionary_barType.is_ob, dictionary_barType.barType_name, event.barType_time, event.is_food, event.food_type, event.food_time, dictionary_age.age_name, dictionary_age.age_description, event.ticketCo_id, event.ticket_locator, event.ticket_left, event_description.description, event_highlight.highlight, event.event_tag, event.image_flyer, event.image_flyer_310x425, event.image_2, event.image_2_310x208, event.image_3, event.image_3_310x208

FROM event

LEFT JOIN venue
    ON venue.venue_id=event.venue_id
LEFT JOIN dictionary_city
    ON venue.city_id=dictionary_city.city_id
LEFT JOIN dictionary_filter
    ON venue.venueType_id=dictionary_filter.filter_id
LEFT JOIN dictionary_barType
    ON event.barType_id=dictionary_barType.barType_id
LEFT JOIN dictionary_age
    ON event.event_age_id=dictionary_age.age_id
LEFT JOIN event_highlight
    ON event_highlight.event_id=event.event_id  
LEFT JOIN event_description
    ON event_description.event_id=event.event_id        

WHERE event.event_id="".$event_id."";

当我在PhP Admin中运行查询时,它工作正常,但当我将代码添加到我的网站时,我收到以下错误

  

“警告:mysql_fetch_array()期望参数1是资源,   给出的布尔值   /home/nye/public_html/theballdrop.com/templates/template.event.php on   第29行 - 更多信息:   http://theballdrop.com/new-years-eve-madame-tussauds/#sthash.880jgbaa.dpuf

它引用的查询是"if ($row = mysql_fetch_array($result)){"

我不知道从哪里开始。这应该在单个页面上显示来自多个表的信息。 $ event_id是变量,特定事件ID将确定要为该特定事件页面显示的信息。

请帮忙。

===========================

以下是该页面的完整代码:

    <?php 

    // FUNCTION: CONVERT DATE
    include (PATH . DIR_INCLUDE . "convert-time.php");

    // CONNECT TO NYE DATABASE
    include (PATH . DIR_TEMPLATE . "mysql-connect.php");

    $sql = "SELECT event.event_id, venue.venue_name, venue.alt_venue_name, venue.venue_address1, venue.cross_street, dictionary_city.city_name, venue.zip, dictionary_filter.filter_name, venue.capacity, event.start_date, event.end_date, dictionary_barType.is_ob, dictionary_barType.barType_name, event.barType_time, event.is_food, event.food_type, event.food_time, dictionary_age.age_name, dictionary_age.age_description, event.ticketCo_id, event.ticket_locator, event.ticket_left, event_description.description, event_highlight.highlight, event.event_tag, event.image_flyer, event.image_flyer_310x425, event.image_2, event.image_2_310x208, event.image_3, event.image_3_310x208 FROM nye_newyears.event LEFT JOIN venue ON venue.venue_id=event.venue_id LEFT JOIN dictionary_city ON venue.city_id=dictionary_city.city_id LEFT JOIN dictionary_filter ON venue.venueType_id=dictionary_filter.filter_id LEFT JOIN dictionary_barType ON event.barType_id=dictionary_barType.barType_id LEFT JOIN dictionary_age ON event.event_age_id=dictionary_age.age_id LEFT JOIN event_highlight ON event_highlight.event_id=event.event_id LEFT JOIN event_description ON event_description.event_id=event.event_id WHERE event.event_id=".$event_id."";

    $result = mysql_query($sql, $con);

    // START MYSQL QUERY        
    if ($row = mysql_fetch_array($result)){

    // VENUE INFORMATION
    $venueName          =   $row['venue_name'];         // OFFICIAL VENUE NAME
    $venueName2         =   $row['alt_venue_name'];     // NAME YOU WANT TO DISPLAY AT TOP OF EVENT PAGE
    $venueAddress       =   $row['venue_address1'];     // ADDRESS OF VENUE
    $venueCrossStreet   =   $row['cross_street'];       // CROSS STREETS
    $venueCity          =   $row['city_name'];          // VENUE CITY
    $venueState         =   $row['state_abb'];          // VENUE STATE
    $venueZip           =   $row['zip'];                // VENUE ZIP
    $venueType          =   $row['filter_name'];        // TYPE OF VENUE
    $venueCapacity      =   $row['capacity'];           // CAPACITY OF VENUE

    // EVENT INFORMATION
    $start              =   $row['start_date'];
    $startDate          =   convertDate ($start);       // START DATE OF EVENT
    $startTime          =   convertTime ($start);       // START TIME OF EVENT
    $end                =   $row['end_date'];
    $endDate            =   convertDate ($end);         // END DATE OF EVENT
    $endTime            =   convertTime ($end);         // END TIME OF EVENT
    $ob                 =   $row['is_ob'];              // IS THERE OPEN BAR? "YES" OR "NO"
    $obType             =   $row['barType_name'];       // TYPE OF OPEN BAR
    $obTimes            =   $row['barType_time'];       // OPEN BAR TIMES (START AND END)
    $food               =   $row['is_food'];            // IS THERE FOOD? "YES" OR "NO"
    $foodType           =   $row['food_type'];          // TYPE OF FOOD SERVED
    $foodTimes          =   $row['food_time'];          // TIMES FOOD BEING SERVED (START AND END)
    $eventAge           =   $row['age_name'];           // AGE RESTRICTION (IF ANY)
    $eventAge_descrip   =   $row['age_description'];
    $ticketCompany      =   $row['ticketCo_id'];        
    $ticketID           =   $row['ticket_locator'];     // WIDGET ID
    $ticketsLeft        =   $row['ticket_left'];        // TOTAL TICKETS LEFT
    $eventDescription   =   $row['description'];        // EVENT DESCRIPTION
    $eventHighlight     =   $row['highlight'];          // EVENT HIGHLIGHT
    $eventTag           =   $row['event_tag'];          // EVENT LISTING DESCRIPTION

    // EVENT IMAGES
    $flyer              =   $row['image_flyer'];            // FULL SIZE EVENT FLYER IMAGE
    $flyer_310x425      =   $row['image_flyer_310x425'];    // FLYER IMAGE - 310x425
    $image2             =   $row['image_2'];                // FULL SIZE IMAGE 2
    $image2_310x208     =   $row['image_2_310x208'];        // IMAGE 2 - 310x208
    $image3             =   $row['image_3'];                // FULL SIZE IMAGE 3
    $image3_310x208     =   $row['image_3_310x208'];        // IMAGE 3 - 310x208

}

    // VENUE MAP
    $venueMap ="$venueAddress+$venueCity+$venueState+$venueZip" ;

    // CLOSE DATABASE
    mysql_close($con);

    // INCLUDE GLOBAL HEADER
    include (PATH . DIR_INCLUDE . "site-header.php"); 

?>

<body>
<?php include_once(PATH . "analyticstracking.php") ?>
<div id="wrapper">
  <?php include (PATH . DIR_INCLUDE . "header.php"); ?>
  <div class="row">
    <div class="middle">
      <div class="wrapper event-header">
        <div class="left twoThird slogan"> <br />
          <h1>
            <?=$venueName2?>
          </h1>
          <h5>
            <?=$eventTag?>
          </h5>
        </div>
        <div class="left oneThird last"> <a class="buy-now" href="#tickets" ><span>Buy Now</span></a> <a href="#" class="greybutton">Ask a Question</a> </div>

        <!-- End: button --> 

      </div>
    </div>

    <!--End .middle -->

    <div class="wrapper">
      <div class="twoThird left columns">
        <div class="roundCorner">
          <div class="widget data flier-section">
            <div class="flier left"> <a href="<?=DIR_IMAGES?><?=$flyer?>" class="flyerzoom"><img src="<?=DIR_IMAGES?><?=$flyer_310x425?>" alt="<?=$venueName2?> New Years Eve 2014" width="310" height="425" /></a> </div>

            <!-- End @ left-->

            <div class="photos right"> <a href="<?=DIR_IMAGES?><?=$image2?>" class="flyerzoom"><img src="<?=DIR_IMAGES?><?=$image2_310x208?>" alt="<?=$venueName?> Times Square New Years Eve 2014" /></a> <a href="<?=DIR_IMAGES?><?=$image3?>" class="flyerzoom fit"><img src="<?=DIR_IMAGES?><?=$image3_310x208?>" alt="<?=$venueName?> Times Square New Years Eve" /></a> </div>

            <!-- End @ right slider--> 

          </div>

          <!-- End @ widget-->

          <div class="center detail">
            <div class="widget lightgray-base">
              <h3 class="widget-heading">Why We Recommend</h3>
              <ul class="blist">
                <?=$eventHighlight?>
              </ul>
            </div>
            <div class="widget">
              <h3 class="headingbar">Overview</h3>
              <?=$eventDescription?>
              <p><strong>About your Tickets:&nbsp;</strong> (1) Show up early and help reduce the wait time, (2) Make sure to have your physical ticket on hand because you will need to show it to NYPD and security upon request, (3) You accept the risks and agree to adhere to the NYPD rules and regulations for the evening, (4) If you select shipping as your delivery method, tickets will not be sent out until December, (5) A LIVE VIEW of The Ball Drop is not guaranteed. Large crowds should be expected</p>
            </div>
          </div>
        </div>

        <!-- End of main-->

        <div class="roundCorner"> <a name="tickets" ></a>
          <h3 class="widget-heading">Purchase Tickets
            <div class="info">Have a Question? Call <a href="tel:2122010735">212.201.0735</a> or email <a href="mailto:info@balldrop.com">info@balldrop.com</a> </div>
          </h3>
          <div class="widget data">
            <p>
              <?php 

                if ($ticketCompany == 1){



                    // START: CRAVE TICKETS WIDGET -->

                    echo "<iframe scrolling='auto' frameborder='0' width='99.5%' height='600' src='http://cravetickets.com/widgets/iframe/qU4owWdNmYt/".$ticketID."' ></iframe>";

                    // END: CRAVE TICKETS WIDGET -->



                } else if ($ticketCompany == 2){



                        // START: LAUGHSTUB TICKET WIDGET -->

                        echo "<script type='text/javascript' src='http://www.laughstub.com/embed/cart/index.cfm?showTimingID=".$ticketID."&bgcolor=E53D23&fontcolor=ffffff&&brand=ticketmob&displayTitle=no&affiliate=1087&checkout=pmtix'></script>";

                        // END: LAUGHSTUB TICKET WIDGET -->             

                    }

            ?>
            </p>
            <p><strong> Price increase warning:</strong> New Year's Eve ticket prices often go up significantly (100% +) the closer you get to New Years Eve. Also note that many NYE Events will sell out well in advance of New Years Eve. </p>
          </div>
        </div>
      </div>

      <!-- End @ twoThird-->

      <div class="oneThird sidebar last">
        <div class="roundCorner lightgray-base">
          <h3 class="widget-heading">What to know</h3>
          <div class="box data">
            <h4>Share This Event</h4>
            <p> <span class='st_facebook_large' displayText='Facebook'></span> <span class='st_twitter_large' displayText='Tweet'></span> <span class='st_plusone_large' displayText='Google +1'></span> <span class='st_linkedin_large' displayText='LinkedIn'></span> <span class='st_pinterest_large' displayText='Pinterest'></span> <span class='st_email_large' displayText='Email'></span> </p>
            <ul class="know-list">
              <li>
                <h5>START TIME</h5>
                <h4>
                  <?=$startDate?>
                  at
                  <?=$startTime?>
                </h4>
              </li>
              <li>
                <h5>END TIME</h5>
                <h4>
                  <?=$endDate?>
                  at
                  <?=$endTime?>
                </h4>
              </li>
              <li>
                <h5>ADDRESS</h5>
                <h4>
                  <?=$venueAddress?>
                  <br>
                  <?=$venueCrossStreet?>
                </h4>
              </li>
              <li>
                <h5>VENUE TYPE</h5>
                <h4>
                  <?=$venueType?>
                </h4>
              </li>
              <li>
                <h5>AGE REQUIREMENT</h5>
                <h4>
                  <?=$eventAge?>
                  <br />
                  <?=$eventAge_descrip?>
                </h4>
              </li>
              <li>
                <h5>CAPACITY</h5>
                <h4>
                  <?=$venueCapacity?>
                </h4>
              </li>
              <li>
                <h5>OPEN BAR</h5>
                <h4>
                  <?=$ob?>
                  |
                  <?=$obType?>
                  <br />
                  from
                  <?=$obTimes?>
                </h4>
              </li>
              <li>
                <h5>FOOD SERVED</h5>
                <h4>
                  <?=$food?>
                  |
                  <?=$foodType?>
                  <br />
                  from
                  <?=$foodTimes?>
                </h4>
              </li>
            </ul>
          </div>
        </div>

        <!-- End of roundCorner -->

        <div class="roundCorner lightgray-base">
          <h3 class="widget-heading">Map</h3>
          <div class="box"> <img src="http://maps.googleapis.com/maps/api/staticmap?center=<?=$venueMap?>&zoom=16&size=295x245&markers=size:mid%7Ccolor:red%7C<?=$venueMap?>&sensor=false">
            <div class="address"> <br />
              <h3>
                <?=$venueName?>
              </h3>
              <h5>
                <?=$venueAddress?>
                <br />
                <?=$venueCrossStreet?>
                <br />
                <?=$venueCity?>
                ,
                <?=$venueState?>
                <?=$venueZip?>
              </h5>
            </div>
          </div>
        </div>

        <!-- End of roundCorner -->

        <div class="banner-300"> <img class="fullWidth" src="<?=DIR_IMAGES?>banner-300.jpg"> </div>

        <!-- End @ Banner--> 

      </div>

      <!-- End of onethird--> 

    </div>

    <!-- End @ wrapper-->

    <div id="footer-top"> 

      <!-- START: SIMILAR PARTIES -->

      <div class="listing-wrapper">
        <h2 class="subHeading">We Recommend <a href="#" class="right view-all">More NYE Parties <strong>&#8250;</strong></a> </h2>
        <ul class="columns">
          <?php include (PATH . DIR_INCLUDE . "similar-parties.php"); ?>
        </ul>
      </div>

      <!-- END: SIMILAR PARTIES --> 

    </div>

    <!-- End @ footer-top -->

    <?php include (PATH . DIR_INCLUDE . "footer.php"); ?>
  </div>

  <!-- End @ row--> 

</div>

<!-- End @ wrapper-->

</body>
</html>

4 个答案:

答案 0 :(得分:1)

将数据库选择添加到连接脚本,如下所示:

<?php
$hostname = "localhost";
$username = "{username]";
$password = "[pass]";
$dbname = "nye_newyears";

$con = mysql_connect("$hostname", "$username", "$password");
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

if(!mysql_select_db($dbname)) {
    die('Could not find or access database ' . $dbname);
}
?>

答案 1 :(得分:0)

event是mysql中的关键字。所以将它包装在引号(`)中并尝试。

在浏览器中回复您的$sql并将其粘贴到phpmyadmin并执行,如果有的话,您将收到特定的错误。

答案 2 :(得分:0)

首先,检查服务器中的MySql表名。有时某些主机会在表名上添加前缀。

您应首先检查$ result对象

if(!$result){
  exit(mysql_error());
}else{
  if ($row = mysql_fetch_array($result)){
   // ....
   // ...
  }
}

答案 3 :(得分:0)

谢谢大家的帮助。 Knittl你是对的!!!

我将以下代码添加到mysql-connect文件中:

if(!mysql_select_db($dbname)) {
    die('Could not find database ' . $dbname);
}
现在一切正常。

非常感谢你。