Advertisement
gridcube

Untitled

Sep 16th, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3.  
  4. <?php
  5.                     function fetch_records($table_name, $fields){
  6.        
  7.                 $conn = mysql_connect ("host", "user", "pass");
  8.                 $select_db = mysql_select_db("base", $conn);
  9.            
  10.                 if(!$conn){
  11.                     return "Not able to connect to dabase....";
  12.                 }
  13.                
  14.                 $table = "<table align='center' border='1' cellpadding='2' cellspacing='2'>";
  15.                 $cols = implode (', ', $fields);
  16.                 $sql = "select $cols from $table_name";  
  17.                 $run = mysql_query($sql);    
  18.                    
  19.                     if($run){    
  20.                         if(mysql_num_rows($run) == 0){
  21.                                 return "Sorry. No records found in the database";                        
  22.                         }
  23.                         else {
  24.                                
  25.                             while($arr = mysql_fetch_array($run, MYSQL_ASSOC)){
  26.                                     $table .= "\t\t<tr>\n";
  27.                                    
  28.                                     foreach ($arr as $val_col) {
  29.                                     $table .= "\t\t\t".'<td>'.$val_col.'</td>'."\n";
  30.                                     }
  31.                                        
  32.                                     $table .= "\t\t</tr>\n";                                        
  33.                             }
  34.                             $table .= "</table>";
  35.                             return $table;
  36.                          }
  37.                        
  38.                         mysql_free_result($run);        
  39.                   }
  40.                  
  41.                 echo  mysql_error();
  42.  }
  43.  
  44. //how to call the function
  45.  
  46.         $table = "TestsTable";
  47.         $cols = array("ID", "DATE", "NICK", "TESTS", "STATUS", "Comm_Bugs");
  48.         echo fetch_records($table, $cols);
  49.  
  50.  
  51. ?>
  52. <table align="center">
  53.     <tr>
  54.         <td>
  55.             <FORM METHOD="post">
  56.  
  57.                 <INPUT  TYPE="button"
  58.                         VALUE="BACK"
  59.                         OnClick="history.go( -1 );return true;">
  60.             </FORM>
  61.         </td>
  62.     </tr>
  63. </table>
  64. </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement