PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Tue Aug 10, 2004 3:32 am
Code: Select all
<?php
session_start();
include("database.php");
function extract_user()
{
$user=addslashes($_SESSION[username]);
$query="SELECT * FROM leave WHERE StaffNo ='$user'";
$result=mysql_query($query);
$row_array=mysql_fetch_array($result);
return $row_array;
}
If(?????)
{
echo"You don't have any leave application in process";
exit;
}
?>
What should I put in the If statement to indicate that there are no result matched to the query??
Skittlewidth
Forum Contributor
Posts: 389 Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK
Post
by Skittlewidth » Tue Aug 10, 2004 4:09 am
Use
Code: Select all
<?php
$num = mysql_num_rows($result);
?>
to count how many results were returned, then put if $num == 0 in your if statement.
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Tue Aug 10, 2004 4:19 am
The above answers your immediate question.
Maintainablilty Hint:
I would reecommend you create a "database abstraction layer' or find an existing one (There are lots around). This would mean if you change your database later on, you only need to change that rather than all your code.[/b]
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Tue Aug 10, 2004 4:29 am
my real problem is this:
I can echo all the variables when I delete the if statement.
But when I include the if statement, the result will always show "you don't have any leave application in process.
Here is the full code:
Code: Select all
<?php
session_start();
include("database.php");
function extract_user()
{
$user=addslashes($_SESSION[username]);
$query="SELECT * FROM leave WHERE StaffNo ='$user'";
$result=mysql_query($query);
$row_array=mysql_fetch_array($result);
return $row_array;
$num = mysql_num_rows($result);
}
If($num == 0)
{
echo"You don't have any leave application in process";
}
else {
$row_array=extract_user();
$nama=$row_array['Nama'];
$jawatan=$row_array['Jawatan'];
$staffno=$row_array['StaffNo'];
$jenis=$row_array['Jenis'];
$mula=$row_array['DateMula'];
$tamat=$row_array['DateTamat'];
$mohon=$row_array['DateMohon'];
$refid=$row_array['RefID'];
$status=$row_array['Status'];
$jumlah=$row_array['Jumlah'];
?>
<font color="#671247" size="3" face="Verdana, Arial, Helvetica, sans-serif">
<div align="left">
<table border="0" cellpadding="2" cellspacing="1" width="100%">
<tr>
<td align="left" valign="bottom" width="30%"
bgcolor="#FFFFFF" valign="TOP" marginwidth="12" marginheight="12">
<hr><b><i>STATUS PERMOHONAN CUTI</b></i><hr><br>
</td>
</tr>
</table>
</center></div>
<div align="left">
<table border="0" cellpadding="2" cellspacing="1" width="100%">
<tr>
<td align="left" valign="bottom" width="30%"
bgcolor="#FFFFFF" valign="TOP" marginwidth="12" marginheight="12">
<b>Refferance No:</b>
</td>
<td align="left" valign="top" width="70%"
bgcolor="#FFFFFF" marginwidth="12" marginheight="12">
<strong><?echo "$refid";?></strong>
</td>
</tr>
</table>
</center></div>
<div align="left">
<table border="0" cellpadding="2" cellspacing="1" width="100%">
<tr>
<td align="left" valign="bottom" width="30%"
bgcolor="#FFFFFF" valign="TOP" marginwidth="12" marginheight="12">
<b>Nama:</b>
</td>
<td align="left" valign="top" width="70%"
bgcolor="#FFFFFF" marginwidth="12" marginheight="12">
<?echo "$nama";?>
</td>
</tr>
</table>
</center></div>
<div align="left">
<table border="0" cellpadding="2" cellspacing="1" width="100%">
<tr>
<td align="left" valign="bottom" width="30%"
bgcolor="#FFFFFF" valign="TOP" marginwidth="12" marginheight="12">
<b>Jawatan:</b>
</td>
<td align="left" valign="top" width="70%"
bgcolor="#FFFFFF" marginwidth="12" marginheight="12">
<?echo "$jawatan";?>
</td>
</tr>
</table>
</center></div>
<div align="left">
<table border="0" cellpadding="2" cellspacing="1" width="100%">
<tr>
<td align="left" valign="bottom" width="30%"
bgcolor="#FFFFFF" valign="TOP" marginwidth="12" marginheight="12">
<b>Jenis Cuti:</b>
</td>
<td align="left" valign="top" width="70%"
bgcolor="#FFFFFF" marginwidth="12" marginheight="12">
<?echo "$jenis";?>
</td>
</tr>
</table>
</center></div>
<div align="left">
<table border="0" cellpadding="2" cellspacing="1" width="100%">
<tr>
<td align="left" valign="bottom" width="30%"
bgcolor="#FFFFFF" valign="TOP" marginwidth="12" marginheight="12">
<b>Tarikh Cuti:</b>
</td>
<td align="left" valign="top" width="70%"
bgcolor="#FFFFFF" marginwidth="12" marginheight="12">
<?echo "$mula";?> hingga <?echo "$tamat";?>
</td>
</tr>
</table>
</center></div>
<div align="left">
<table border="0" cellpadding="2" cellspacing="1" width="100%">
<tr>
<td align="left" valign="bottom" width="30%"
bgcolor="#FFFFFF" valign="TOP" marginwidth="12" marginheight="12">
<b>Status:</b>
</td>
<td align="left" valign="top" width="70%"
bgcolor="#FFFFFF" marginwidth="12" marginheight="12">
<strong><?echo "$status";?></strong>
</td>
</tr>
</table>
</center></div>
<?}
?>
Skittlewidth
Forum Contributor
Posts: 389 Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK
Post
by Skittlewidth » Tue Aug 10, 2004 4:57 am
I think thats because you haven't executed the function that sets $num by the time the if statement is called, so $num will by default be 0 or null
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Aug 10, 2004 9:53 am
there's that, and this:
Code: Select all
return $row_array;
$num = mysql_num_rows($result);
mysql_num_rows will never be called with this code, as it's after the return call.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Tue Aug 10, 2004 3:19 pm
Would this work for you?
Code: Select all
<?php
session_start();
include("database.php");
function extract_user()
{
$user=addslashes($_SESSION[username]);
$query="SELECT * FROM leave WHERE StaffNo ='$user'";
$result=mysql_query($query);
$row_array=mysql_fetch_array($result);
//since '0' evaluates to false...
$ret_val = (mysql_num_rows($result)) ? $row_array : false;
return $ret_val;
}
$row_array = extract_user();
if($row_array)
{
$nama=$row_array['Nama'];
$jawatan=$row_array['Jawatan'];
$staffno=$row_array['StaffNo'];
$jenis=$row_array['Jenis'];
$mula=$row_array['DateMula'];
$tamat=$row_array['DateTamat'];
$mohon=$row_array['DateMohon'];
$refid=$row_array['RefID'];
$status=$row_array['Status'];
$jumlah=$row_array['Jumlah'];
else
{
echo"You don't have any leave application in process";
//might wanna stop echoing the rest of the page here.
}
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
g3ckO
Forum Contributor
Posts: 117 Joined: Mon Jul 12, 2004 2:57 am
Location: Malaysia
Contact:
Post
by g3ckO » Tue Aug 10, 2004 8:33 pm
Yups pickle.... its work for me.
Thanks..