Jump to content

Checking for no data in SQL query


rocky48
Go to solution Solved by rocky48,

Recommended Posts

I am trying to code my script to display a message if the SQL query finds no results.

I have used this snippet in a previous script and it works OK, but it does not work in this context.

 

The SQL query looks for data with the same date of today, if it does it adds the data to a table.

 

Here is part of the file where it does the querying:

<?php
include("RelHol_connect.php");
doDB();

//verify the Event exists
$verify_Event_sql = "SELECT Events.ID, Events.Event_Date, Events.Event_Description, Religion.ID, Religion.Religion
	FROM Events
	LEFT JOIN Religion
	ON Religion.ID = Faith_ID
	WHERE MONTHNAME(Events.Event_Date)= MONTHNAME(Now()) And YEAR(Now())
	ORDER BY Events.Event_Date ASC";
$verify_Event_res =  mysqli_query($mysqli, $verify_Event_sql) or die(mysqli_error($mysqli));

if (mysqli_num_rows($verify_Event_res) < 1) {
	//this Event does not exist
	$display_block = "<p><em>No Religious festivals or holidays today.</em></p>";
}else {
//gather the Events
	$get_Event_sql  = "SELECT Events.ID, Events.Event_Date, Events.Event_Description, Events.Faith_ID, Religion.ID, Religion.Religion
	FROM Events
	LEFT JOIN Religion
	ON Events.Faith_ID = Religion.ID
	WHERE DAY(Events.Event_Date)= Day(Now()) And MONTH(Events.Event_Date)=Month(Now()) And YEAR(Events.Event_Date)=YEAR(Now())";

	$get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli));
//create the display string
	$display_block .= "
<table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" >
	<tr>
	<th>RELIGION</th>
	<th>EVENT</th>
	</tr>";	

	while ($Event_today = mysqli_fetch_array($get_Event_res)) {
		$Rel_Date = $Event_today['Event_Date'];
		$Rel_Event = $Event_today['Religion'];
		$Event_text = ($Event_today['Event_Description']);

//add to display
$display_block .= "
<tr>
<td width=\"8%\" valign=\"top\">".$Rel_Event."<br/></td>
<td width=\"25%\" valign=\"top\">" .$Event_text."<br/></td>";	
		
	}	

//free results
	mysqli_free_result($get_Event_res);

//close connection to MySQL
	mysqli_close($mysqli);
	
//close up the table
$display_block .="</table>";
echo $display_block;
}
?>

The first part of the script from '//Verify the event exists' is the code I have added.  Before I added this it just printed the table header, if there was no data or lines of data if there was data.

 

Can anyone help?

Link to comment
Share on other sites

I would start by echoing $verify_Event_sql and running it against the database perhaps in phpMyAdmin to see what results you get. Then do the same with $get_Event_sql. Perhaps the second query is not producing results because it looks like the first query counts all events in the current month (Day is not considered) but

WHERE DAY(Events.Event_Date)= Day(Now()) And MONTH(Events.Event_Date)=Month(Now()) And YEAR(Events.Event_Date)=YEAR(Now())

looks at only events happening today.

Link to comment
Share on other sites

Hi Rocky48

 

I created a database in your structure and tried the code. Apparently the $get_Event_res is returned as an object not an array; to solve this, you need to use the fetch_object() function.

 

change this code:

 

while ($Event_today = mysqli_fetch_array($get_Event_res)) {
        $Rel_Date = $Event_today['Event_Date'];
        $Rel_Event = $Event_today['Religion'];
        $Event_text = ($Event_today['Event_Description']);

 

 

to this:

 

while($Event_today = $get_Event_res->fetch_object()){
        $Rel_Date = $Event_today->Event_Date;
        $Rel_Event = $Event_today->Religion;
        $Event_text = ($Event_today->Event_Description);

 

 

Remember, its an object; not an array.

Link to comment
Share on other sites

Hi DavidAnnis

 

Don't understand why you say it does not check for the day?

 

Without the code I am trying to add it works, and if there is any events today it iterates and fills the table with the events for today.  If there are no events it just displays the table header.  What I am trying to achieve is a message on the days there are no events.  I will try the query on myphpadmin.

 

My website is www.1066cards4u.co.uk and the code runs on the index page.  There are no events today 14th July.  For a list of when events are happenning there is a link on the menu.

 

Lazro - I tried your code but it does not work with my DB.  I am not an expert, but surely if there are multiple fields output by the query it would have to be an array?

 

Hope this clarifies what I am trying to do!

Link to comment
Share on other sites

Opps!

 

I was using a wrong version of the query, so I modified it to: WHERE DAY(Events.Event_Date)= Day(Now()) And MONTH(Events.Event_Date)=Month(Now()) And YEAR(Events.Event_Date)=YEAR(Now())

 

It now does not print the table headers, but it does print the message, when there is no data!

Link to comment
Share on other sites

your first SELECT query is finding any result in the current month with any true year value. that has nothing to do with checking if there is an event on any day.

 

the second query is all you need. you would then test if the number of rows from that query is less than one. if it is, there isn't any event on the current date.

 

also just use WHERE Events.Event_Date = CURDATE() in your queries.

Link to comment
Share on other sites

I copied the wrong WHERE code in my first code listing, but I have now put the modified WHERE code in both queries, but it does not print the text if the number of rows is <1.

 

The first query is to catch this criteria and the second query will print any rows if there is any data tyo output for that date.

 

I have modified as suggested, but it still does not print the message.

 

For clarity I list the code as modified:

<?php
include("RelHol_connect.php");
doDB();

//verify the Event exists
$verify_Event_sql = "SELECT Events.ID, Events.Event_Date, Events.Event_Description, Religion.ID, Religion.Religion
	FROM Events
	LEFT JOIN Religion
	ON Religion.ID = Faith_ID
	WHERE Events.Event_Date = CURDATE()
	ORDER BY Events.Event_Date ASC";
$verify_Event_res =  mysqli_query($mysqli, $verify_Event_sql) or die(mysqli_error($mysqli));

if (mysqli_num_rows($verify_Event_res) < 1) {
	//this Event does not exist
	$display_block = "<p><em>No Religious festivals or holidays today.</em></p>";
}else {
//gather the Events
	$get_Event_sql  = "SELECT Events.ID, Events.Event_Date, Events.Event_Description, Events.Faith_ID, Religion.ID, Religion.Religion
	FROM Events
	LEFT JOIN Religion
	ON Events.Faith_ID = Religion.ID
	WHERE WHERE Events.Event_Date = CURDATE()
	ORDER BY Events.Event_Date ASC";
	$get_Event_res = mysqli_query($mysqli, $get_Event_sql) or die(mysqli_error($mysqli));
//create the display string
	$display_block .= "
<table width=\"100%\" cellpadding=\"3\" cellspacing=\"1\" border=\"1\" BGCOLOR=\"#87CEEB\" >
	<tr>
	<th>RELIGION</th>
	<th>EVENT</th>
	</tr>";	

	while ($Event_today =  mysqli_fetch_array($get_Event_res)) {
		$Rel_Date = $Event_today['Event_Date'];
		$Rel_Event = $Event_today['Religion'];
		$Event_text = ($Event_today['Event_Description']);

//add to display
$display_block .= "
<tr>
<td width=\"8%\" valign=\"top\">".$Rel_Event."<br/></td>
<td width=\"25%\" valign=\"top\">" .$Event_text."<br/></td>";	
		
	}	

//free results
	mysqli_free_result($get_Event_res);

//close connection to MySQL
	mysqli_close($mysqli);
	
//close up the table
$display_block .="</table>";
//echo $display_block;
}
?>

Any more ideas as to why it is not printing the message when there are no rows?

Link to comment
Share on other sites

  • Solution

Corrected the fact that the queries were different.  Also noticed that I had WHERE twice in the second query.

 

Thanks mac_gyver!  You gave me the clue to the solution the Echo $displayblock should have been put just after the error message as if this is true it does not action the second query and therefore does not print the error message if place where I originally had it.

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.