0

I have a total of 42 records in the table. The problem is my one of my column, which indicates the Type of Leave, is call $empType. In my column of that table, the 24th record of $empType is called "Medical Leave" But because of the while loop, the $empType only shows the 42th record, therefore the whole if statement does not work.

I do not want it to show just 24th record, cause I know odbc_fetch_row will work as well, but I want it to loop all the way and capture all the data from each row.

$conn=odbc_connect("employee","","") or die (odbc_errormsg());

$sql1="SELECT * FROM employee WHERE Status='Pending'";
$rs1=odbc_exec($conn,$sql1);
while (odbc_fetch_row($rs1))
{
$leaveID=odbc_result($rs1,"Leave ID");
$empID=odbc_result($rs1,"empID");
$empType=odbc_result($rs1,"TypeOfLeave");
}

if ($status == "Approved" && $empType == "Medical Leave")
{
my code
}

echo $empType;

Can anyone help me get through this? I seriously need to get this done.

I'm using Microsoft access database ODBC.

2
  • The code inside the loop basically does nothing, and the code you want to run on each row is outside the loop. What more is there to say? Commented May 10, 2012 at 9:42
  • I tried putting the while loop so that it ends after my if loop. But it does not work Commented May 10, 2012 at 9:44

3 Answers 3

1
<?php
$conn = odbc_connect("employee","","") or die (odbc_errormsg());

$sql1 = "SELECT * FROM employee WHERE Status='Pending'";
$rs1 = odbc_exec($conn,$sql1);

while(odbc_fetch_row($rs1)) {
    $leaveID=odbc_result($rs1,"Leave ID");
    $empID=odbc_result($rs1,"empID");
    $empType=odbc_result($rs1,"TypeOfLeave");
    $status = odbc_result($rs1,"Status"); // added this.


    // moved to the while loop.
    if( $empType === 'Medical Leave' && $status === 'Approved' ) {
        // your code.
    }
}

Also, PHP's ODBC API looks scary, with all the odbc_fetch_row, odbc_result going on. Perhaps it's a good idea to use PDO for this instead? That way, the code would look like this:

<?php
$dbh = new Pdo( 'odbc:MSSQLServer', 'username', 'password' );

$results = $dbh->query( 'SELECT * FROM employee', PDO::FETCH_ASSOC );

foreach( $results as $result ) {
    if( $result['TypeOfLeave'] === 'Medical Leave' && $result['Status'] === 'Approved' ) {
        // your code here.
    }
}

I've not tried using PDO with ODBC so I'm not familiar with bugs, but from what I can tell; any other API than the one you're using is an improvement.

EDIT: If you want to use all rows later (for looping, etc), this is a good alternative:

<?php
$conn = odbc_connect("employee","","") or die (odbc_errormsg());

$sql1 = "SELECT * FROM employee WHERE Status='Pending'";
$rs1 = odbc_exec($conn,$sql1);

$rows = array( );

while(odbc_fetch_row($rs1)) {
    $rows[] = array(
        'leave ID' => odbc_result( $rs1, 'Leave ID' ),
        'empID' => odbc_result( $rs1, 'empID' ),
        'empType' => odbc_result( $rs1, 'empType' ),
        'status' => odbc_result( $rs1, 'Status' ),
    );
}

// $rows now contains *all* rows, which you can loop over later.

// some more code here.

foreach( $rows as $row ) {
    if( $row['status'] === 'Approved' && 'empType' === 'Medical Leave' ) {
        // your code here.
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@user127886 No problem. I do see a small issue with the code, though: your query contains "WHERE Status = 'Pending'". Later on, we're checking $status == 'Approved'. It can never be 'Approved', because those results are filtered from the query.
0

Well as Jon pointed out, your if needs to be inside the while, but also you never define $status so the if would never run no matter where it is

2 Comments

I forgot to put the $status in the code. I guess I will have to figure out something out, i'm currently too dazed to do anything, thank you anyway sir :).
Do you mean it's really there, just not in the version you posted? Aside from that you just need to move the closing bracket of the while to the end
0

You're looping through each row of data in that while yet you have an if statement that holds a condition for a var that is re-declared in each loop of the while outside of the while loop...

You need to have the if inside the while:

$conn=odbc_connect("employee","","") or die (odbc_errormsg());

$sql1="SELECT * FROM employee WHERE Status='Pending'";
$rs1=odbc_exec($conn,$sql1);
while (odbc_fetch_row($rs1))
{
$leaveID=odbc_result($rs1,"Leave ID");
$empID=odbc_result($rs1,"empID");
$empType=odbc_result($rs1,"TypeOfLeave");

if ($status == "Approved" && $empType == "Medical Leave")
{
my code
}//end of if

}//end of while

echo $empType;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.