Advertisement
Guest User

Untitled

a guest
Jul 7th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.98 KB | None | 0 0
  1. <?php
  2.  
  3. $hostname = "localhost";
  4. $dbuser = "thestory_carl";
  5. $dbpassword = "carl";
  6. $dbname = "thestory_mancstore";
  7.  
  8.      $db_link=mysql_connect($hostname, $dbuser, $dbpassword)
  9.      or die("Unable to connect to the server!");
  10.  
  11.      mysql_select_db($dbname)
  12.      or die("Unable to connect to the database");
  13.  
  14.         $fields_array=array();
  15.         $num_fields=0;
  16.         $num_row=0;
  17.        
  18.  
  19.         $sql="select * from customers ";
  20.  
  21. // find position of "FROM" in query
  22.         $fpos=strpos($sql, 'from');
  23.  
  24.         // get string starting from the first word after "FROM"
  25.         $strfrom=substr($sql, $fpos+5, 50);
  26.  
  27.         // Find position of the first space after the first word in the string
  28.         $Opos=strpos($strfrom,' ');
  29.  
  30.         //Get table name. If query pull data from more then one table only first table name will be read.
  31.         $table=substr($strfrom, 0,$Opos);
  32.  
  33.            // Get result from query
  34.             $result=mysql_query($sql);
  35.             $num_row=mysql_numrows($result);
  36.  
  37.             print('<html>');
  38.             print('<head><title>');
  39.             print('View&nbsp'.$table.'</title>');
  40.            
  41.  
  42.             print("</head>");
  43.             print('<body><br>');
  44.  
  45.             if($num_row >0)
  46.             {
  47.                     //Get number of fields in query
  48.                     $num_fields=mysql_num_fields($result);
  49.  
  50.          
  51.  
  52.            # get column metadata
  53.            $i = 0;
  54.  
  55.              //Set table width 15% for each column
  56.             $width=10 * $num_fields;
  57.  
  58.             print('<br><table width='.$width.'% align="center"><tr>');
  59.             print('<tr><th colspan='.$num_fields.'>View&nbsp'.$table.'</th></tr>');
  60.  
  61.              while ($i < $num_fields)
  62.              {
  63.  
  64.               //Get fields (columns) names
  65.             $meta = mysql_fetch_field($result);
  66.  
  67.             $fields_array[]=$meta->name;
  68.  
  69.            //Display column headers in upper case
  70.        print('<th><b>'.strtoupper($fields_array[$i]).'</b></th>');
  71.  
  72.                     $i=$i+1;
  73.                     }
  74.  
  75.             print('</tr>');
  76.                
  77.  
  78.                    //Get values for each row and column
  79.                 while($row=mysql_fetch_row($result))
  80.                 {
  81.                  print('<tr>');
  82.  
  83.                         for($i=0; $i<$num_fields; $i++)
  84.                         {
  85.                         //Display values for each row and column
  86.                         print('<td>'.$row[$i].'</td>');
  87.  
  88.                         }
  89.  
  90.                 print('</tr>');
  91.                 }
  92.  
  93.     }
  94.  
  95.  
  96.      $db_link=mysql_connect($hostname, $dbuser, $dbpassword)
  97.      or die("Unable to connect to the server!");
  98.  
  99.      mysql_select_db($dbname)
  100.      or die("Unable to connect to the database");
  101.  
  102.         $fields_array=array();
  103.         $num_fields=0;
  104.         $num_row=0;
  105.        
  106.  
  107.         $sql="select * from customers ";
  108.  
  109. // find position of "FROM" in query
  110.         $fpos=strpos($sql, 'from');
  111.  
  112.         // get string starting from the first word after "FROM"
  113.         $strfrom=substr($sql, $fpos+5, 50);
  114.  
  115.         // Find position of the first space after the first word in the string
  116.         $Opos=strpos($strfrom,' ');
  117.  
  118.         //Get table name. If query pull data from more then one table only first table name will be read.
  119.         $table=substr($strfrom, 0,$Opos);
  120.  
  121.            // Get result from query
  122.             $result=mysql_query($sql);
  123.             $num_row=mysql_numrows($result);
  124.  
  125.             print('<html>');
  126.             print('<head><title>');
  127.             print('View&nbsp'.$table.'</title>');
  128.            
  129.  
  130.             print("</head>");
  131.             print('<body><br>');
  132.  
  133.             if($num_row >0)
  134.             {
  135.                     //Get number of fields in query
  136.                     $num_fields=mysql_num_fields($result);
  137.  
  138.          
  139.  
  140.            # get column metadata
  141.            $i = 0;
  142.  
  143.              //Set table width 15% for each column
  144.             $width=10 * $num_fields;
  145.  
  146.             print('<br><table width='.$width.'% align="center"><tr>');
  147.             print('<tr><th colspan='.$num_fields.'>View&nbsp'.$table.'</th></tr>');
  148.  
  149.              while ($i < $num_fields)
  150.              {
  151.  
  152.               //Get fields (columns) names
  153.             $meta = mysql_fetch_field($result);
  154.  
  155.             $fields_array[]=$meta->name;
  156.  
  157.            //Display column headers in upper case
  158.        print('<th><b>'.strtoupper($fields_array[$i]).'</b></th>');
  159.  
  160.                     $i=$i+1;
  161.                     }
  162.  
  163.             print('</tr>');
  164.                
  165.  
  166.                    //Get values for each row and column
  167.                 while($row=mysql_fetch_row($result))
  168.                 {
  169.                  print('<tr>');
  170.  
  171.                         for($i=0; $i<$num_fields; $i++)
  172.                         {
  173.                         //Display values for each row and column
  174.                         print('<td>'.$row[$i].'</td>');
  175.  
  176.                         }
  177.  
  178.                 print('</tr>');
  179.                 }
  180.  
  181.     }
  182.  
  183.  
  184.  
  185.     ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement