Round Posted January 17, 2007 Share Posted January 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/34571-odbc-if-while-loop-help/ Share on other sites More sharing options...
Round Posted January 17, 2007 Author Share Posted January 17, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/34571-odbc-if-while-loop-help/#findComment-162860 Share on other sites More sharing options...
newladder Posted February 16, 2007 Share Posted February 16, 2007 Hi Round,i do have the same problem now,did you find the solution for this ?Regards Quote Link to comment https://forums.phpfreaks.com/topic/34571-odbc-if-while-loop-help/#findComment-186170 Share on other sites More sharing options...
Round Posted March 27, 2007 Author Share Posted March 27, 2007 Hello newladder,Sorry it's been a while. Yes I did find a solution, if it's still an issue you have too, I'll find the code and help you out.Cheers Quote Link to comment https://forums.phpfreaks.com/topic/34571-odbc-if-while-loop-help/#findComment-216074 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.