简体   繁体   English

左联接多个表

[英]Left join for multiple tables

I am trying to execute the following MySQL query: 我正在尝试执行以下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."";

When I run the query in PhP Admin it works fine, but when I add the code to my site, I get the following error 当我在PhP Admin中运行查询时,它可以正常工作,但是当我将代码添加到我的网站时,出现以下错误

"Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /home/nye/public_html/theballdrop.com/templates/template.event.php on line 29 - See more at: http://theballdrop.com/new-years-eve-madame-tussauds/#sthash.880jgbaa.dpuf " “警告:mysql_fetch_array()期望参数1为资源,在第29行的/home/nye/public_html/theballdrop.com/templates/template.event.php中给定的布尔值-请参见: http ://theballdrop.com/ 除夕夫人杜莎夫人蜡像馆/#sthash.880jgbaa.dpuf

The query it refers to is "if ($row = mysql_fetch_array($result)){" 它引用的查询是"if ($row = mysql_fetch_array($result)){"

I am not sure where to go from here. 我不确定从这里去哪里。 This is supposed to display information from multiple tables on a single page. 应该在单个页面上显示来自多个表的信息。 $event_id is the variable and a specific event ID would determine the info to display for that specific event page. $ event_id是变量,特定的事件ID将确定要为该特定事件页面显示的信息。

please help. 请帮忙。

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

Here is the entire code for the page: 这是页面的完整代码:

    <?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>

Add database selection to your connect script, like so: 将数据库选择添加到您的连接脚本中,如下所示:

<?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);
}
?>

event is a keyword in mysql. event是mysql中的关键字。 so wrap it inside quotes (`) and try. 因此请将其包装在引号(`)中并尝试。

echo your $sql in browser copy it and paste it in phpmyadmin and execute, you will get the particular error if any. 在浏览器中回显$sql ,将其复制并粘贴到phpmyadmin中并执行,您将得到特定的错误(如果有)。

At first, check your MySql Table name in server. 首先,在服务器中检查您的MySql表名称。 Sometimes some hosts add a prefix on table name. 有时某些主机在表名上添加前缀。

You should check $result object first like 您应该先检查$ result对象,例如

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

Thank you all for your help. 谢谢大家的帮助。 Knittl you were RIGHT!!! Knittl你说得对!

I added the following code to mysql-connect file: 我将以下代码添加到mysql-connect文件中:

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

everything works now. 现在一切正常。

Thank you so much. 非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM