dmirsch Posted February 24, 2012 Share Posted February 24, 2012 I have a template page that opens with a GO click from a drop-down in a different page. The variable is being passed through to the URL and in the GET statement. However, my PHP code is producing the following error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /data/23/2/100/53/2263053/user/2487001/htdocs/myalaskacenter/events/venues/test_venues-results.php on line 52. Here is my code: <?php //Assigns the venueCode tothe variable with a more convenient name $venueURL = $_GET['VenueCode']; @$DB = mysqli_connect('server','username','password','database'); if (mysqli_connect_errno()){ echo '</blockquote><br/><br /> Sorry, this webpage is temporarily unavailable.<br /> <a href="http://alaskapac.centertix.net/">Click here to search for events.</a>'; } else { ?> <?php $Query = "SELECT Events.EventTitle, DATE_FORMAT(Performance.startDateTime, '%W, %M %e, %Y') AS startDate, DATE_FORMAT(Performance.startDateTime, '%h:%i %p') AS startTime, Events.EventID, Events.thumb, Events.ShoWareEventLink, Events.tagline, Performance.category_id, Promoters.Presenter, Promoters.website, Events.startDATE, Events.endDATE, Events.EventOnSaleDate, venues.VenueName FROM ((Events LEFT JOIN Performance ON Events.EventID = Performance.EventID) LEFT JOIN Promoters ON Events.PromoterCode = Promoters.PromoterCode) LEFT JOIN venues ON Performance.VenueCode = venues.VenueCode WHERE venues.VenueCode=".$venueURL." AND Events.group_id=1 AND Performance.category_id!=2 AND Performance.category_id!=5 AND Performance.category_id!=7 AND Performance.category_id!=8 AND Events.EventOnSaleDate IS NOT NULL AND (Performance.PerfType='Public Event' OR Events.EventID='79') AND Performance.endDateTime >= now()-INTERVAL 1 DAY AND Events.PublishDate <= now() AND Events.startDATE IS NOT NULL ORDER BY Performance.startDateTime"; $Result = mysqli_query($DB,$Query); $NumResults = mysqli_num_rows($Result); ?> <?php if($NumResults=0){ echo "<p class='submenu'>$NumResults Performances</p>"; } while ($Row = mysqli_fetch_assoc($Result)){ $eventtitle = $Row['EventTitle']; $eventDate = $Row['startDate']; echo '<p><a href="' . $Row['ShoWareEventLink'] . '"><img src="https://alaskapac.centertix.net/UPLImage/' . $Row['thumb'] . '" alt="' . $Row['EventTitle'] . '" title="' . $Row['EventTitle'] . '" align="left" border="0"><span class="Heading3_blue">' . $Row['EventTitle'] . '</span>'; if($Row['FreeEvents']==TRUE){ echo '<img src="/images/free.gif" alt="Free Event" title="Free Event" width="80" height="80" align="right" border="0"></a><br />'; } elseif ($Row['EventOnSaleDate'] <= date("Y-m-d g:i a")){ /** IF ONSALEDATE<=NOW**/ echo '<img src="/images/logos/ctx/BUY_Tickets_gold.gif" alt="Buy Tickets" title="Buy Tickets" width="85" height="32" align="right" border="0"></a><br />'; } else {/** IF ONSALEDATE!<NOW**/ echo '<img src="/images/logos/ctx/AvailableSoon.png" alt="Available Soon" title="Available Soon" align="right" border="0"></a><br /><i>Tickets available ' . date("l, F j, Y", strtotime($Row['EventOnSaleDate'])) . ' at ' . date("g:i a", strtotime($Row['EventOnSaleDate'])) . '.</i>'; /** +ONSALEDATE **/ } echo '<br />Presented by <a href="' . $Row['website'] .'" target="_blank">' . $Row['Presenter'] . '</a>'; echo '<br />'.$Row['startDate']. ' at ' . $Row['startTime'].' - '.$Row['VenueName']; if ($Row['FreeEvents']==TRUE){ echo '<br /><br />'.$Row['BriefDescription']; } echo '<br /><br /></p><hr>'; } ?> <?php mysqli_free_result($Result); mysqli_close($DB); } ?> Here is my sample Results page: http://www.myalaskacenter.com/events/venues/test_venues-results.php?VenueCode=ACH Quote Link to comment https://forums.phpfreaks.com/topic/257660-variable-in-query-works-but-php-does-not/ Share on other sites More sharing options...
Zane Posted February 24, 2012 Share Posted February 24, 2012 Anytime you have an error like this Warning: mysqli_some_function expects parameter 1 to be mysqli_something, boolean given That boolean is most likely FALSE. You would only receive a FALSE value in a mysql(i) function if the query failed. Quote Link to comment https://forums.phpfreaks.com/topic/257660-variable-in-query-works-but-php-does-not/#findComment-1320594 Share on other sites More sharing options...
darkfreaks Posted February 24, 2012 Share Posted February 24, 2012 Zane is correct you have a date which acts as a boolean. which makes it FALSE. so your query fails. i think it could however work with mysqli_fetch_assoc Quote Link to comment https://forums.phpfreaks.com/topic/257660-variable-in-query-works-but-php-does-not/#findComment-1320599 Share on other sites More sharing options...
dmirsch Posted February 24, 2012 Author Share Posted February 24, 2012 When I removed the variable from the SQL query and replace it with a value, the PHP code works fine. So maybe it's not a PHP issue. What I am trying to do is use the variable that was picked and shows up in my URL and my $_GET statement as demonstrated on my sample page. Once I put the variable into the SQL query that is when I get the error message. Perhaps I am putting that into the SQL query wrong. However, when I put the SQL with the variable into my phpMyAdmin page, it does not produce an error. Also darkfreaks suggested I use a different mysqli statement, but I'm not sure where to place it. I tried to put that into $NumResults = mysqli_num_rows($Result); instead of the mysqli_num_rows, but I still get the same error. Quote Link to comment https://forums.phpfreaks.com/topic/257660-variable-in-query-works-but-php-does-not/#findComment-1320604 Share on other sites More sharing options...
Pikachu2000 Posted February 24, 2012 Share Posted February 24, 2012 @darkfreaks - A date that acts as a boolean? What does that mean? I'm assuming the $venueURL variable holds string type data. It isn't quoted in the query string. It also DESPERATELY needs to be validated and sanitized before you use it in a query string. Change: . . . WHERE venues.VenueCode=".$venueURL." AND . . . To: WHERE venues.VenueCode= '$venueURL' AND Quote Link to comment https://forums.phpfreaks.com/topic/257660-variable-in-query-works-but-php-does-not/#findComment-1320612 Share on other sites More sharing options...
dmirsch Posted February 24, 2012 Author Share Posted February 24, 2012 THANK YOU Pikachu2000!!! I think you've helped me before! I very much appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/257660-variable-in-query-works-but-php-does-not/#findComment-1320615 Share on other sites More sharing options...
PFMaBiSmAd Posted February 24, 2012 Share Posted February 24, 2012 Your VenueCode is a string and would need to be enclosed in single-quotes within the query statement. (edit: which Pikachu2000 pointed out above.) I also notice you are doing a less-than-or-equal-to date comparison with a date() format of "Y-m-d g:i a". The less-than part of that won't work for a couple of reasons - 1) The g (12 hr time w/o leading zero) and a (am or pm) produce values that are not ordered by magnitude (i.e. 1 pm will be less-than 9 am, 11 am is less-than than 8 am, ...). The hours needs to be 24 hour with leading zero and the am/pm is not needed. 2) Your EventOnSaleDate column is apparently a mysql datetime data type (YYYY-MM-DD HH:MM:SS) (if it's not, it needs to be for a greater-than/less-than comparison to work) and you would need to use a date() format of 'Y-m-d H:i:s' in order for a greater-than/less-than comparison to work. Quote Link to comment https://forums.phpfreaks.com/topic/257660-variable-in-query-works-but-php-does-not/#findComment-1320616 Share on other sites More sharing options...
dmirsch Posted February 24, 2012 Author Share Posted February 24, 2012 Thank you PFMaBiSmAd, I made the changes you suggested. I think you've also helped me before! I very much appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/257660-variable-in-query-works-but-php-does-not/#findComment-1320624 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.