Jump to content

Variable in Query works, but PHP does not


dmirsch

Recommended Posts

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

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.