Page 1 of 1

query checking

Posted: Tue Aug 10, 2004 3:32 am
by g3ckO

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??

Posted: Tue Aug 10, 2004 4:09 am
by Skittlewidth
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.

Posted: Tue Aug 10, 2004 4:19 am
by CoderGoblin
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]

Posted: Tue Aug 10, 2004 4:29 am
by g3ckO
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>

<?}
?>

Posted: Tue Aug 10, 2004 4:57 am
by Skittlewidth
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

Posted: Tue Aug 10, 2004 9:53 am
by feyd
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.

Posted: Tue Aug 10, 2004 3:19 pm
by pickle
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.
}   
?>

Posted: Tue Aug 10, 2004 8:33 pm
by g3ckO
Yups pickle.... its work for me.

Thanks.. :)