Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @author Jeff Tanner, jeff_tanner@earthlink.net
- *
- * 1. Connect to a localhost MySQL database.
- * 2. Select database
- * 3. Request to perform SQL query
- * 4. Dynamically create TABLE from returned results.
- * 5. Close connection.
- */
- // Connect to localhost @ port 3306
- $link = mysql_connect("localhost:3306","root","sifra");
- if (!$link) {
- die('Could not connect: ' . mysql_error());
- }
- echo nl2br( "Connected successfully!\n" );
- echo nl2br( sprintf("MySQL host info: %s\n", mysql_get_host_info()) );
- echo nl2br( sprintf("MySQL server version: %s\n", mysql_get_server_info()) );
- echo nl2br( sprintf("MySQL client info: %s\n", mysql_get_client_info()) );
- linebreak();
- // Select database
- $db = mysql_select_db("baya", $link);
- if (!$db) {
- die('Could not open database: ' . mysql_error());
- }
- // create SQL statement for TABLE artist.
- createDynamicHTMLTable("bus1", "SELECT * FROM bus1", $link);
- // free resources and close connection
- mysql_close($link);
- /**
- * Create a dynamic table with headers based on the column names
- * from a query. It automatically creates the table and the correct
- * number of columns.
- */
- function createDynamicHTMLTable($table_name, $sql_query, $link)
- {
- // execute SQL query and get result
- $sql_result = mysql_query($sql_query, $link);
- if (($sql_result)||(mysql_errno == 0))
- {
- echo "<DIV>\n";
- linebreak( strong( sprintf("Table: \"%s\"", $table_name) ) );
- echo "<TABLE borderColor=#000000 cellSpacing=0 cellPadding=6 border=2>\n";
- echo "<TBODY>\n";
- if (mysql_num_rows($sql_result)>0)
- {
- //loop thru the field names to print the correct headers
- $i = 0;
- echo "<TR vAlign=top bgColor=#00ffff>\n";
- while ($i < mysql_num_fields($sql_result))
- {
- echo "<TH>". mysql_field_name($sql_result, $i) . "</TH>\n";
- $i++;
- }
- echo "</TR>\n";
- //display The data
- while ($rows = mysql_fetch_array($sql_result,MYSQL_ASSOC))
- {
- echo "<TR>\n";
- foreach ($rows as $data)
- {
- echo "<TD align='center'>". $data . "</TD>\n";
- }
- echo "</TR>\n";
- }
- } else {
- echo "<TR>\n<TD colspan='" . ($i+1) . "'>No Results found!</TD></TR>\n";
- }
- echo "</TBODY>\n</TABLE>\n";
- echo "</DIV>\n";
- } else {
- echo nl2br( sprintf( "Error in running query: %s\n", mysql_error()) );
- }
- mysql_free_result($sql_result);
- linebreak();
- }
- /**
- * Wraps text with HTML tag STRONG.
- */
- function strong($text)
- {
- return "<STRONG>$text</STRONG>\n";
- }
- /**
- * Ends text with HTML tag BR.
- * Default return just BR.
- */
- function linebreak($text="\n") {
- echo nl2br( $text );
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement