Jump to content


Photo

Checking for no data in SQL query


Best Answer rocky48, 17 July 2013 - 12:24 PM

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.

Go to the full post


  • Please log in to reply
11 replies to this topic

#1 rocky48

rocky48

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts
  • LocationHastings, UK
  • Age:66

Posted 14 July 2013 - 07:05 AM

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?


Rocky48

#2 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 592 posts
  • LocationOkemos, MI USA

Posted 14 July 2013 - 08:26 AM

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.



#3 Lazro

Lazro

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 14 July 2013 - 08:44 AM

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.



#4 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 592 posts
  • LocationOkemos, MI USA

Posted 14 July 2013 - 09:08 AM

Lazro. I'm not a MySQL expert so I don't understand how you know his database structure. All you have is the field names from his query. What am I missing that allows you to duplicate the database?



#5 rocky48

rocky48

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts
  • LocationHastings, UK
  • Age:66

Posted 14 July 2013 - 10:25 AM

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!


Rocky48

#6 rocky48

rocky48

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts
  • LocationHastings, UK
  • Age:66

Posted 14 July 2013 - 11:00 AM

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!


Rocky48

#7 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,448 posts

Posted 14 July 2013 - 11:02 AM

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.


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting. 


#8 rocky48

rocky48

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts
  • LocationHastings, UK
  • Age:66

Posted 14 July 2013 - 12:36 PM

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?


Rocky48

#9 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 592 posts
  • LocationOkemos, MI USA

Posted 14 July 2013 - 07:25 PM

The display block in the error condition does not have a <tr> <td> tags and is displayed i the middle of the table. Perhaps the browser doesn't know how to display it. View source and see if it is there.



#10 mac_gyver

mac_gyver

    Advanced Member

  • Administrators
  • 2,448 posts

Posted 15 July 2013 - 08:34 AM

i've got to ask - why are you running the same query twice and are you getting any output at all because you have commented out the //echo $display_block; statement that would be needed to display the Event does not exist message?


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting. 


#11 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 592 posts
  • LocationOkemos, MI USA

Posted 15 July 2013 - 10:01 AM

Why does the second query contain Events.Faith_ID but the first does not (the Join is different too)?



#12 rocky48

rocky48

    Advanced Member

  • Members
  • PipPipPip
  • 99 posts
  • LocationHastings, UK
  • Age:66

Posted 17 July 2013 - 12:24 PM   Best Answer

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.


Rocky48




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com