Advertisement
Guest User

Baya

a guest
Sep 26th, 2014
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.94 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author Jeff Tanner, jeff_tanner@earthlink.net
  4.  *
  5.  * 1. Connect to a localhost MySQL database.
  6.  * 2. Select database
  7.  * 3. Request to perform SQL query
  8.  * 4. Dynamically create TABLE from returned results.
  9.  * 5. Close connection.
  10.  */
  11.  
  12. // Connect to localhost @ port 3306
  13. $link = mysql_connect("localhost:3306","root","sifra");
  14. if (!$link) {
  15.     die('Could not connect: ' . mysql_error());
  16. }
  17. echo nl2br( "Connected successfully!\n" );
  18. echo nl2br( sprintf("MySQL host info: %s\n", mysql_get_host_info()) );
  19. echo nl2br( sprintf("MySQL server version: %s\n", mysql_get_server_info()) );
  20. echo nl2br( sprintf("MySQL client info: %s\n", mysql_get_client_info()) );
  21. linebreak();
  22.    
  23. // Select database
  24. $db = mysql_select_db("baya", $link);
  25. if (!$db) {
  26.     die('Could not open database: ' . mysql_error());
  27. }
  28.  
  29. // create SQL statement for TABLE artist.
  30. createDynamicHTMLTable("bus1", "SELECT * FROM bus1", $link);
  31.  
  32.  
  33. // free resources and close connection
  34. mysql_close($link);
  35.  
  36. /**
  37.  * Create a dynamic table with headers based on the column names
  38.  * from a query. It automatically creates the table and the correct
  39.  * number of columns.
  40.  */
  41. function createDynamicHTMLTable($table_name, $sql_query, $link)
  42. {
  43.     // execute SQL query and get result
  44.     $sql_result = mysql_query($sql_query, $link);
  45.     if (($sql_result)||(mysql_errno == 0))
  46.     {        
  47.         echo "<DIV>\n";
  48.         linebreak( strong( sprintf("Table: \"%s\"", $table_name) ) );
  49.         echo "<TABLE borderColor=#000000 cellSpacing=0 cellPadding=6 border=2>\n";
  50.         echo "<TBODY>\n";
  51.         if (mysql_num_rows($sql_result)>0)
  52.         {
  53.             //loop thru the field names to print the correct headers
  54.             $i = 0;
  55.             echo "<TR vAlign=top bgColor=#00ffff>\n";
  56.             while ($i < mysql_num_fields($sql_result))
  57.             {
  58.                 echo "<TH>". mysql_field_name($sql_result, $i) . "</TH>\n";
  59.                 $i++;
  60.             }
  61.             echo "</TR>\n";
  62.  
  63.             //display The data
  64.             while ($rows = mysql_fetch_array($sql_result,MYSQL_ASSOC))
  65.             {
  66.                 echo "<TR>\n";
  67.                 foreach ($rows as $data)
  68.                 {
  69.                     echo "<TD align='center'>". $data . "</TD>\n";
  70.                 }
  71.                 echo "</TR>\n";
  72.             }
  73.         } else {
  74.             echo "<TR>\n<TD colspan='" . ($i+1) . "'>No Results found!</TD></TR>\n";
  75.         }
  76.        
  77.         echo "</TBODY>\n</TABLE>\n";
  78.         echo "</DIV>\n";
  79.     } else {
  80.         echo nl2br( sprintf( "Error in running query: %s\n", mysql_error()) );
  81.     }
  82.     mysql_free_result($sql_result);
  83.     linebreak();
  84. }
  85.  
  86. /**
  87.  * Wraps text with HTML tag STRONG.
  88.  */
  89. function strong($text)
  90. {
  91.     return "<STRONG>$text</STRONG>\n";
  92. }
  93.  
  94. /**
  95.  * Ends text with HTML tag BR.
  96.  * Default return just BR.
  97.  */
  98. function linebreak($text="\n") {
  99.     echo nl2br( $text );
  100. }
  101. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement