PHP mysql query help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acpjax
Forum Newbie
Posts: 12
Joined: Fri Nov 10, 2006 11:02 am

PHP mysql query help

Post by acpjax »

Quick question i have the blow my sql query set up and i need some help adding an extra criteria

Code: Select all

$sql = mysql_query("SELECT * FROM calendar_events WHERE event_date = '$year-$month-$day'"); 
if (mysql_num_rows($sql) > 0) { 
  while ($e = mysql_fetch_array($sql)) { 
    echo "<p>$e[event_avalibility]</p>\n"; 
  } 
} else { 
  echo "<p>No events today</p>\n"; 
}
I currently have a variable $listingid and for the sake of this moment lets say the value is 10
and a field in the table called listing_id

i want to add a extra criteria to the above query that says
SELECT * FROM calendar_events WHERE event_date = '$year-$month-$day
and
where listing_id = $listingid

so basicly i want all event dates where listing_id = $listingid

im just not sure how to write it in. any help would be appricated
User avatar
Jaxolotl
Forum Contributor
Posts: 137
Joined: Mon Nov 13, 2006 4:19 am
Location: Argentina and Italy

Post by Jaxolotl »

Code: Select all

$sql = mysql_query("
SELECT *
FROM calendar_events
WHERE event_date = '".$year."-".$month."-".$day."'
AND listing_id = ".$listingid."
");
if (mysql_num_rows($sql) > 0) {
  while ($e = mysql_fetch_array($sql)) {
    echo "<p>$e[event_avalibility]</p>\n";
  }
} else {
  echo "<p>No events today</p>\n";
}
two suggestions.
1 - Use the list of field when making a query insted of use * for two reasons, by using * you may retrive more information than you need, and a list of the fields make the code easy to read

2 - $e[event_avalibility] works but make notice error messages if event_avalibility is not a constant, if is just a key for the array write it between ' $e['event_avalibility']

is this what your where looking for? if not please let me know, I'm a bit tired today
acpjax
Forum Newbie
Posts: 12
Joined: Fri Nov 10, 2006 11:02 am

Post by acpjax »

I feel like an idiot but i need to add the same condtion to the below statment... help would be appreciated

AND event_listingid = $listingid when i try and add that it dosent work

Code: Select all

function getEventDays($month, $year) { 
  $days = array(); 
  $sql = mysql_query("SELECT DAY(event_date) AS day, COUNT(event_id) FROM calendar_events WHERE MONTH(event_date) = '$month' AND YEAR(event_date) = '$year' GROUP BY day"); 

  if (mysql_num_rows($sql) > 0) { 
  while ($row = mysql_fetch_array($sql)) $days[] = $row['day']; 
  } 

  return $days; 
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Where did you add it?
acpjax
Forum Newbie
Posts: 12
Joined: Fri Nov 10, 2006 11:02 am

Post by acpjax »

ive tried in a few places

Code: Select all

function getEventDays($month, $year) { 
  $days = array(); 
  $sql = mysql_query("SELECT DAY(event_date) AS day, COUNT(event_id) FROM calendar_events WHERE MONTH(event_date) = '$month' AND YEAR(event_date) = '$year' **this is where i tried to add** GROUP BY day");
Post Reply