Page 1 of 1

PHP mysql query help

Posted: Wed Nov 22, 2006 10:22 am
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

Posted: Wed Nov 22, 2006 10:33 am
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

Posted: Thu Nov 23, 2006 7:38 pm
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; 
}

Posted: Thu Nov 23, 2006 7:54 pm
by feyd
Where did you add it?

Posted: Thu Nov 23, 2006 8:18 pm
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");