Jump to content

php odbc function:


jguy

Recommended Posts

Hello all.  I'm having trouble with a function that I'm trying to design.  The idea here is for the function to receive a SQL string, connect to the database, run the query, and return all data encapsulated within an HTML table.  I have a great deal of this function completed.  However, here's the problem that I'm having.  I wish to use this function site wide, so I'm creating a .inc file to include it on each page needing a dynamic table.  With this said, each table returned will have a different number of fields each time.  Within the function, I'm not sure how to properly display all of the field names as <th> and then iterate through the data in the <td> fields within this function. 

Here's the code:
[code]
<?php
/*
  Function to receive strSQL statement and
  return a HTML table.
*/
function fnSQLtoHTML($strSQL){
if (!$conn = odbc_connect('mydb','myuser','mypass')){
  $sRetVal = "<b><font color=red>The Database Connection Failed.<br> Contact Your Systems Administrator:</font></b>";
}else{
if (!$rs = odbc_exec($conn, $strSQL)){
  $sRetVal = "<b>Error in your SQL statement</b>";
  $sRetVal .= "<br />".$strSQL;
}else{
$sRetVal = "<table width=\"56%\" align=\"center\" border=\"1\" padding=\"5\" cellspacing=\"5\" bgcolor=\"#FFFFFF\">\n";
$sRetVal .= "<tr><th colspan=".odbc_num_fields($rs) .">";
$sRetVal .= odbc_field_name($rs,0)."</th></tr>";
$sRetVal .= "<tr>";
$i=0;
  while ($i < odbc_num_fields($rs)){
  $sRetVal .="<th>".odbc_field_name($rs,$i)."</th>";
  $i++;
  }
$sRetVal .= "</tr>";
  while ($line = odbc_fetch_row($rs)){
  $sRetVal .= "\t<tr>\n";
      foreach ($line as $col_value) {
  $sRetVal .= "\t\t<td>$col_value</td>\n";
  }
  $sRetVal .= "\t</tr>\n";
  }
$sRetVal .= "</table>\n";
}
}
return ($sRetVal);
}
?>
[/code]

What I'm getting right now is 1 field name in the <th> and a blank html table.  Can someone maybe point me in the right direction?  Many thanks in advance!
Link to comment
https://forums.phpfreaks.com/topic/32878-php-odbc-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.