Jump to content

PHP MySQL calendar


wrathican

Recommended Posts

hi there, i followed the basic event calendar tutorial located on this website;

http://www.phpfreaks.com/tutorials/144/4.php

 

i get this sql error;

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/fhlinux179/l/ls12style.co.uk/user/htdocs/test/calendar.php on line 24

 

this is my line 24;

if (mysql_num_rows($sql) > 0) {

 

i added some code to print the mysql error and i got this;

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(event_date) AS day, COUNT(event_id) FROM calendar_events WHERE

 

any suggestions on what's going wrong?

my database has been populated with some basic events so my query shouldnt fail.

 

thanx in advance.

 

Link to comment
Share on other sites

oh sorry, i did mean to put that in...

here ya go:

 

$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"); 

Link to comment
Share on other sites

well i copy and pasted the whole code at the end of the tutorial, to be honest i didnt think to check for typos but here it is, ill have a look myself and see if i can see any

<?php
// Database connection variables 
// Be sure to set these to your own connection 
$HOST = 'host'; 
$USER = 'user'; 
$PASS = 'pass'; 
$NAME = 'ls12style'; 

// Connect to MySQL database 
$con = mysql_connect($HOST, $USER, $PASS); 
if (!$con) { 
  // Since the entire script depends on connection, die if connection fails 
  die("Error connecting to MySQL database!"); 
} 
mysql_select_db($NAME, $con); 

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(!$sql)
{
echo "theres has been an error: " . mysql_error();
}
  while ($row = mysql_fetch_array($sql)) $days[] = $row['day']; 
  } 

  return $days; 
} 

function drawCalendar($month, $year) { 
  // set variables we will need to help with layouts 
  $first       = mktime(0,0,0,$month,1,$year); // timestamp for first of the month 
  $offset      = date('w', $first); // what day of the week we start counting on 
  $daysInMonth = date('t', $first); 
  $monthName   = date('F', $first); 
  $weekDays    = array('Su', 'M', 'Tu', 'W', 'Th', 'F', 'Sa'); 
  $eventDays   = getEventDays($month, $year); 
   
  // Start drawing calendar 
  $out  = "<table id=\"myCalendar\">\n"; 
  $out .= "<tr><th colspan=\"7\">$monthName $year</th></tr>\n"; 
  $out .= "<tr>\n"; 
  foreach ($weekDays as $wd) $out .= "<td class=\"weekDays\">$wd</td>\n"; 
   
  $i = 0; 
  for ($d = (1 - $offset); $d <= $daysInMonth; $d++) { 
    if ($i % 7 == 0) $out .= "<tr>\n"; // Start new row 
    if ($d < 1) $out .= "<td class=\"nonMonthDay\"> </td>\n"; 
    else { 
      if (in_array($d, $eventDays)) { 
        $out .= "<td class=\"monthDay\">\n"; 
        $out .= "<a href=\"?year=$year&month=$month&day=$d\">$d</a>\n"; 
        $out .= "</td>\n"; 
      } else $out .= "<td class=\"monthDay\">$d</td>\n"; 
    } 
    ++$i; // Increment position counter 
    if ($i % 7 == 0) $out .= "</tr>\n"; // End row on the 7th day 
  } 
   
  // Round out last row if we don't have a full week 
  if ($i % 7 != 0) { 
    for ($j = 0; $j < (7 - ($i % 7)); $j++) { 
      $out .= "<td class=\"nonMonthDay\"> </td>\n"; 
    } 
    $out .= "</tr>\n"; 
  } 
   
  $out .= "</table>\n"; 
     
    return $out; 
} 

$year  = isset($_GET['year']) ? $_GET['year'] : date('Y'); 
$month = isset($_GET['month']) ? $_GET['month'] : date('m'); 

echo drawCalendar($month, $year); 

// Previous month link 
$prevTS = strtotime("$year-$month-01 -1 month"); // timestamp of the first of last month 
$pMax = date('t', $prevTS); 
$pDay = ($day > $pMax) ? $pMax : $day; 
list($y, $m) = explode('-', date('Y-m', $prevTS)); 
echo "<p>\n"; 
echo "<a href=\"?year=$year&month=$m&day=$pDay\">« Prev</a> |\n"; 

// Next month link 
$nextTS = strtotime("$year-$month-01 +1 month"); 
$nMax = date('t', $nextTS); 
$nDay = ($day > $nMax) ? $nMax : $day; 
list($y, $m) = explode('-', date('Y-m', $nextTS)); 
echo "<a href=\"?year=$y&month=$m&day=$nDay\">Next »</a>\n"; 
echo "</p>\n"; 

// Calendar is done, let's list events for selected day 
$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_title]</p>\n"; 
  } 
} else { 
  echo "<p>No events today</p>\n"; 
} 
?>

Link to comment
Share on other sites

yeah i still get the same error:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '(event_date) AS day, COUNT(event_id) FROM calendar_events WHERE

 

i took the query right from the tutorial

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.