rocky48 Posted July 14, 2013 Share Posted July 14, 2013 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? Quote Link to comment Share on other sites More sharing options...
davidannis Posted July 14, 2013 Share Posted July 14, 2013 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. Quote Link to comment Share on other sites More sharing options...
Lazro Posted July 14, 2013 Share Posted July 14, 2013 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. Quote Link to comment Share on other sites More sharing options...
davidannis Posted July 14, 2013 Share Posted July 14, 2013 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? Quote Link to comment Share on other sites More sharing options...
rocky48 Posted July 14, 2013 Author Share Posted July 14, 2013 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! Quote Link to comment Share on other sites More sharing options...
rocky48 Posted July 14, 2013 Author Share Posted July 14, 2013 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! Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 14, 2013 Share Posted July 14, 2013 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. Quote Link to comment Share on other sites More sharing options...
rocky48 Posted July 14, 2013 Author Share Posted July 14, 2013 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? Quote Link to comment Share on other sites More sharing options...
davidannis Posted July 15, 2013 Share Posted July 15, 2013 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. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted July 15, 2013 Share Posted July 15, 2013 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? Quote Link to comment Share on other sites More sharing options...
davidannis Posted July 15, 2013 Share Posted July 15, 2013 Why does the second query contain Events.Faith_ID but the first does not (the Join is different too)? Quote Link to comment Share on other sites More sharing options...
Solution rocky48 Posted July 17, 2013 Author Solution Share Posted July 17, 2013 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.