Can anybody see what I am doing wrong here?
I am trying to include a certain page depending on the value of a field in database.
I have 2 tables to check,
if username appears in table.1 and field.units = days inlcude days.php
if username appears in table.1 and field.units = hours inlcude hours.php
if username appears in table.2 and field.units = days inlcude days.php
if username appears in table.2 and field.units = hours inlcude hours.php
$username = $USER->firstname.' '.$USER->lastname;
echo $username;
$is_academic_result = mysql_query('SELECT * from holiday_entitlement_academic where employee = '.$username.'');
$is_business_result = mysql_query('SELECT * from holiday_entitlement_business_manual where employee = '.$username.'');
if(mysql_num_rows($is_academic_result) > 0){
while($is_academic = mysql_fetch_array($is_academic_result)) {
if ($is_academic['units'] == 'days'){include('days.php');}
else if ($is_academic['units'] == 'hours'){include('hours.php');}
}
}
else if(mysql_num_rows($is_business_result) > 0){
while($is_business = mysql_fetch_array($is_business_result)) {
if ($is_business['units'] == 'days'){include('days.php');}
else if ($is_business['units'] == 'hours'){include('hours.php');}
}
}
First of all, you don’t need to do any of these operations in
whileloops, since there will only ever be one or zero results returned (you’re checking the primary key, right?).Secondly, your query isn’t set up correctly – you’re using single-quotes but never escaping them.
So, with that in mind, we do the following:
PLEASE NOTE You should stop using
mysql_*functions. They’re being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you’re not sure which one to use, read this SO article.EDIT If you’re unsure where the problem lies, you can always
echoyour query to make sure you’re passing what you think you’re passing to the database (more often than not, when a query isn’t working, it’s either this, or your logic is bad).