Help required with error message

Im getting this error “Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\WEBAPP 07\calendar.php on line 132”, can someone help me with it?

<?php
# errors weergeven
ini_set('display_errors',1); // 1 == aan , 0 == uit
error_reporting(E_ALL | E_STRICT);
?>
<?php
session_start();
include('./database.php');
error_reporting(E_ALL ^ E_DEPRECATED);
?>
<html>
<head>
    <script>
        function goLastMonth(month, year){
            if(month == 1) {
                --year;
                month = 13;
            }
            --month
            var monthstring= ""+month+"";
            var monthlength = monthstring.length;
            if(monthlength <=1){
                monthstring = "0" + monthstring;
            }
            document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
        }
        function goNextMonth(month, year){
            if(month == 12) {
                ++year;
                month = 0;
            }
            ++month
            var monthstring= ""+month+"";
            var monthlength = monthstring.length;
            if(monthlength <=1){
                monthstring = "0" + monthstring;
            }
            document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
        }
    </script>
    <style>
        .today{
            background-color: #00ff00;
        }
        .event{
            background-color: #FF8080;
        }
    </style>
</head>
<body>
<?php
if (isset($_GET['day'])){
    $day = $_GET['day'];
} else {
    $day = date("j");
}
if(isset($_GET['month'])){
    $month = $_GET['month'];
} else {
    $month = date("n");
}
if(isset($_GET['year'])){
    $year = $_GET['year'];
}else{
    $year = date("Y");
}
$currentTimeStamp = strtotime( "$day-$month-$year");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>
<?php
if(isset($_GET['add'])){
    $title =$_POST['txttitle'];
    $detail =$_POST['txtdetail'];
    $eventdate = $month."/".$day."/".$year;
    $sqlinsert = "INSERT into eventcalendar(Title,Detail,eventDate,dateAdded) values ('".$title."','".$detail."','".$eventdate."',now())";
    $resultinginsert = mysql_query($sqlinsert);
    if($resultinginsert ){
        echo "Event was successfully Added...";
    }else{
        echo "Event Failed to be Added....";
    }
}
?>

<table border='0'>
    <tr>
        <td><input style='width:50px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.",".$year?>)"></td>
        <td colspan='5'><?php echo $monthName.", ".$year; ?></td>
        <td><input style='width:50px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.",".$year?>)"></td>
    </tr>
    <tr>
        <td width='50px'>Sun</td>
        <td width='50px'>Mon</td>
        <td width='50px'>Tue</td>
        <td width='50px'>Wed</td>
        <td width='50px'>Thu</td>
        <td width='50px'>Fri</td>
        <td width='50px'>Sat</td>
    </tr>
    <?php
    echo "<tr>";
    for($i = 1; $i < $numDays+1; $i++, $counter++){
        $timeStamp = strtotime("$year-$month-$i");
        if($i == 1) {
            $firstDay = date("w", $timeStamp);
            for($j = 0; $j < $firstDay; $j++, $counter++) {
                echo "<td>&nbsp;</td>";
            }
        }
        if($counter % 7 == 0) {
            echo"</tr><tr>";
        }
        $monthstring = $month;
        $monthlength = strlen($monthstring);
        $daystring = $i;
        $daylength = strlen($daystring);
        if($monthlength <= 1){
            $monthstring = "0".$monthstring;
        }
        if($daylength <=1){
            $daystring = "0".$daystring;
        }
        $todaysDate = date("m/d/Y");
        $dateToCompare = $monthstring. '/' . $daystring. '/' . $year;
        echo "<td align='center' ";
        if ($todaysDate == $dateToCompare){
            echo "class ='today'";
        } else{
            $sqlCount = "select * from eventcalendar where eventDate='".$dateToCompare."'";
            $noOfEvent = mysql_num_rows(mysql_query($sqlCount));
            if($noOfEvent >= 1){
                echo "class='event'";
            }
        }
        echo "><a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a></td>";
    }
    echo "</tr>";
    ?>
</table>
<?php
if(isset($_GET['v'])) {
    echo "<hr>";
    echo "<a href='".$_SERVER['PHP_SELF']."?month=".$month."&day=".$day."&year=".$year."&v=true&f=true'>Add Event</a>";
    if(isset($_GET['f'])) {
        include("eventform.php");
    }
    $sqlEvent = "select * FROM eventcalendar where eventDate='".$month."/".$day."/".$year."'";
    $resultEvents = mysql_query($sqlEvent);
    echo "<hr>";
    while ($events = mysql_fetch_array($resultEvents)){
        echo "Title: ".$events['Title']."<br>";
        echo "Detail: ".$events['Detail']."<br>";
    }
}
?>
</body>
</html>

Welcome, @Nathanbotter. It’s easier for us to read your code if you format it properly. I can do it this time for you, but next time, just place three backticks (`) on the line before the code, and three backticks on the line after the code. Or you could just highlight the code, and select the </> icon above the post edit box.

By the way, your PHP is way out of date. The mysql_ functions are no longer part of PHP, so you should upgrade your code to use either mysqli_ or better still, PDO. Be careful when you switch over - the mysqli_ functions are not the same as mysql_ functions, so you can’t just get away with adding an ‘i’ after the mysql.

Ahh okay thanks will use that next time.

mysql_num_rows can only receive a boolean when mysql_query returns a boolean, which means mysql_query has return false (because it never return true), so there must be an error in your query.

To debug, change it to

$sqlCount = "select * from eventcalendar where eventDate='".$dateToCompare."'";
$result = mysql_query($sqlCount) || die(mysql_error());
$noOfEvent = mysql_num_rows($result);

That will stop processing and show the error on screen. do this for local testing only, not in production!

Also, you should not be using the mysql_ functions anymore, as they’ve been deprecated in PHP 5.5 and have been removed altogether from PHP 7.0. Instead switch to either mysqli_ or PDO, the latter being my weapon of choice as the API is nicer.

Lastly, a hint:

can also be written as

$monthstring = str_pad($month, 2, '0', STR_PAD_LEFT);

Which means take the input string $month, and if its length is less than 2, pad it with zeroes (‘0’) on the left (STR_PAD_LEFT) until the length is 2.

So you get 01, 02, 03, 04 … 10, 11, 12, 13, etc

As well as the mysql functions being out-of-date, the way you are using them is very dangerous and open the sql injection attacks.
Never place un-cleaned data from things like $_GET variables directly into a query.
When you make the move to mysqli or PDO you can make use of prepared statements to solve this problem.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.