Jump to content

Recommended Posts

Hello all,

I am trying to carry out two if statements with an odbc connection. I want to check the first table for results and display, if there are any, if not check another table and display results if there are any from that table. If there still isn't any results then display the message "there are no records".

The counts and if statements are working because it will display the echo's I have placed at intervals within the code but it will not carry out the while fetch arrays and display the tables.

Here is my code

[code]<?php
$conn = odbc_connect('name','username','password');
      if(!$conn) {exit("Err:Conn"); }

$sql = "select * from new_table where ID='$id' ";

$rs = odbc_exec($conn, $sql);
if( !$rs )
{
exit ("Could not execute Query");
}
$count=0;
    while($temp = odbc_fetch_into($rs,&$counter)){
        $count++;
  }
if ( $count !=0 )
{
echo ("records found in new table");
#retrieve data
while ( $row = odbc_fetch_array( $rs ) )
{
$list = "<table align= \"center\" border=\"0\" cellpadding=\"4\" width=\"100%\">";
$list .= "<tr>";
$list .= "<th class=table>Column1</th>";
$list .= "<th class=table>Column2</th>";
$list .= "<th class=table width=\"30%\">Column3</th>";
$list .= "<th class=table width=\"40%\">Column4</th>";
$list .= "<th class=table>Column5</th>";
$list .= "</tr>";
$list .= "<tr>";
$list .= "<td class=table>".$row4["Column1"]."</td>";
$list .= "<td class=table>".$row4["Column2"]."</td>";
$list .= "<td class=table width=\"30%\">".$row4["Column3"]."</td>";
$list .= "<td class=table width=\"40%\">".$row4["Column4"]."</td>";
$list .= "<td class=table>".$row4["Column5"]."</td></tr><tr>";
$list .= "<th class=table colspan =\"5\">Column6</th></tr><tr>";
$list .= "<td class=table colspan =\"5\">".$row4["Column6"]."</td>";
$list .= "</tr>";
}
$list .= "</table>";

echo( $list. "<br>" );
}
else
{
odbc_free_result($rs);
#create query
$sql2 = "select * from archive where ID='$id' ";

#exe query
$rs2 = odbc_exec($conn, $sql2);
if( !$rs2 )
{
exit ("Could not execute Query 2");
}

$count=0;
    while($temp = odbc_fetch_into($rs2,&$counter)){
        $count++;
  }
if ( $count !=0 )
{
echo ("No records in new table, records found in archive");
while ( $row2 = odbc_fetch_row( $rs2 ) )
{
$list2 = "<table align= \"center\" border=\"0\" cellpadding=\"4\" width=\"100%\">";
$list2 .= "<tr>";
$list2 .= "<th class=table>Column1</th>";
$list2 .= "<th class=table>Column2</th>";
$list2 .= "<th class=table width=\"40%\">Column3</th>";
$list2 .= "<th class=table width=\"50%\">Column4</th>";
$list2.= "</tr>";
$list2 .= "<tr>";
$list2 .= "<td class=table>".$row5["Column1"]."</td>";
$list2 .= "<td class=table>".$row5["Column2"]."</td>";
$list2 .= "<td class=table width=\"40%\">".$row5["Column3"]."</td>";
$list2 .= "<td class=table width=\"50%\">".$row5["Column4"]."</td>";
$list2 .= "</tr>";
}
$list2 .= "</table>";

echo( $list2. "<br>" );
}
else
{
echo ("<table align= \"center\" border=\"0\" cellpadding=\"4\" width=\"100%\">
<tr>
<th class=table>There are no records</th>
</tr>
</table>");
}}
odbc_free_result($rs2);
?>[/code]

Can anyone shed some light??

Many Thanks
Link to comment
https://forums.phpfreaks.com/topic/34571-odbc-if-while-loop-help/
Share on other sites

Forgot to mention, I am using the count because I was originally using odbc_num_rows to check for results but it would only ever return the result -1. I have also tried removing the while loop like so:-
[code]$count=0;
    while($temp = odbc_fetch_into($rs,&$counter)){
        $count++;
   }
if ( $count !=0 )
{
echo ("records found in new table");
#retrieve data

$list = "<table align= \"center\" border=\"0\" cellpadding=\"4\" width=\"100%\">";
$list .= "<tr>";
$list .= "<th class=table>Column1</th>";
$list .= "<th class=table>Column2</th>";
$list .= "<th class=table width=\"30%\">Column3</th>";
$list .= "<th class=table width=\"40%\">Column4</th>";
$list .= "<th class=table>Column5</th>";
$list .= "</tr>";
$list .= "<tr>";
$list .= "<td class=table>".$column1 = odbc_result($rs, "column1")."</td>";
$list .= "<td class=table>".$column2 = odbc_result($rs, "column2")."</td>";
$list .= "<td class=table width=\"30%\">".$column3 = odbc_result($rs, "column3")."</td>";
$list .= "<td class=table width=\"40%\">".$column4 = odbc_result($rs, "column4")."</td>";
$list .= "<td class=table>".$column5 = odbc_result($rs, "column5")."</td></tr><tr>";
$list .= "<th class=table colspan =\"5\">Column6<th></tr><tr>";
$list .= "<td class=table colspan =\"5\">".$column6 = odbc_result($rs, "column6")."</td>";
$list .= "</tr>";

$list .= "</table>";

echo( $list. "<br>" );
[/code]

Which does work, but if they have two rows in the table will it only display one of them??
I need the while loop don't I?

Thanks Again
  • 5 weeks later...
  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.