Detecting a blank row upon a return

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
sleazyfrank
Forum Commoner
Posts: 40
Joined: Fri Aug 19, 2005 3:59 am
Location: Horsham, West Sussex

Detecting a blank row upon a return

Post by sleazyfrank »

Hi all; I have this code that pulls out records from my DB where courseId matches in my courses and schedule table:

Code: Select all

$getDatesAssociatedWithThisCourse = "SELECT tblCourses.courseID,  
tblCourses.duration, tblSchedule.day, tblSchedule.monthID, tblSchedule.yearID, 
tblYear.year
FROM tblCourses, tblSchedule, tblYear, tblMonth
WHERE tblCourses.courseID = tblSchedule.courseID 
AND tblSchedule.yearID = tblYear.yearID 
AND tblSchedule.monthID = tblMonth.monthID
AND tblCourses.courseID = ".$link." 
AND tblMonth.monthID = ".$idx." ORDER BY monthID ASC";
												
//echo $getDatesAssociatedWithThisCourse;
												
$dboutput_dates = mysql_query($getDatesAssociatedWithThisCourse);
The code works fine if a match is made, but it falls over when no match is made. There is no error, but I'm writing the results into a table row, so when there is no match I want it to write out a blank cell, if there is, write out the cell and put in the result. The latter all works fine, but for the life of me I cannot get the IF to work that says "If $dboutput_dates contains no matching data, just write out a blank cell".

I've tried if ($dboutput_dates == false){ and if ($dboutput_dates == null){ and a few others but nothing.
As I am new to php/mysql, can I assume that even if there is no match made, something is returned? ie my IF will never be true? If so, how do I detect a 0 match?

cheers for any help

Frank
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

if (empty($var)) {
//$var is empty
} else {
//$var is not empty
}
sleazyfrank
Forum Commoner
Posts: 40
Joined: Fri Aug 19, 2005 3:59 am
Location: Horsham, West Sussex

Post by sleazyfrank »

Thanks for the reply which I coded in without success and I think I know why..... the sql is saying Get a bunch of rows together where the courseID and the monthID in these two tables that match these two vars. So if they don't match, they will never be added to the sql return :(

I proved this to my self by echoing out the $day var that is extracted from the Schedule table and they all appear one after the other as I have asked for them, except there are no gaps - because the db ignores any non-matching records. Damn. Again empty() will never be true.

I've also tried to see if it were possible in my loop of pull data from the RS into appropriate vars and print those details only if the months match, and do a blank cell if they don't - but again the months will always match because the SQL will chuck away any records that don't match before the RS is returned.

The only solution I can see is to run the sql for every date stored in the schedule table and any RS that came back blank will be an empty cell, but that is a stupidly server heavy thing to do and I doubt my ISP will like being hammered in that way seeing as we offer quite a few courses....

Hmm, this is a toughie for a newbie like me! :)

F
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Change your SQL. :)

Try using JOIN's instead of all those WHERE's :)
sleazyfrank
Forum Commoner
Posts: 40
Joined: Fri Aug 19, 2005 3:59 am
Location: Horsham, West Sussex

Post by sleazyfrank »

Urk...<clunk>....I knew it was inevitable .... was dreading learning joins

cheers

f
sleazyfrank
Forum Commoner
Posts: 40
Joined: Fri Aug 19, 2005 3:59 am
Location: Horsham, West Sussex

Post by sleazyfrank »

Help.... I've learned a little about basic LEFT JOINS, which I need in this case, but this is a complex sql statement and it would be great if someone could lend me a hand in converting it or providing some pointers so I learn in the process? :oops:

cheers

frank
Post Reply