Guest User

query.php

a guest
Jul 18th, 2016
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.91 KB | None | 0 0
  1.  
  2.  
  3. <?php
  4. $fname =$_POST['fname'];
  5. $query ="select * from student where name='$fname'";
  6.  
  7. if(isset($_POST['submit'])) {
  8.     echo SQLResultTable($query);
  9. }
  10.  
  11.  
  12. function SQLResultTable($Query)
  13. {  
  14.     $username="root";
  15.     $password="";
  16.     $database="school";
  17.  
  18.     $link = mysqli_connect("localhost:3306", $username, $password) or die('Could not connect: ' . mysqli_error());      //build MySQL Link
  19.     mysqli_select_db($link,$database) or die('Could not select database');      //select database
  20.     $Table = "";  //initialize table variable
  21.    
  22.     $Table.= "<table border='1' style=\"border-collapse: collapse;\">"; //Open HTML Table
  23.    
  24.     $Result = mysqli_query($link,$Query); //Execute the query
  25.     if(mysqli_error($link))
  26.     {
  27.         $Table.= "<tr><td>MySQL ERROR: " . mysqli_error($link) . "</td></tr>";
  28.     }
  29.     else
  30.     {
  31.         //Header Row with Field Names
  32.         $NumFields = mysqli_num_fields($Result);
  33.         $Table.= "<tr style=\"background-color: #fe921f; color: #FFFFFF;\">";
  34.         for ($i=0; $i < $NumFields; $i++)
  35.         {    
  36.             $Table.= "<th>" . mysqli_field_name($Result, $i) . "</th>";
  37.         }
  38.         $Table.= "</tr>";
  39.    
  40.         //Loop thru results
  41.         $RowCt = 0; //Row Counter
  42.         while($Row = mysqli_fetch_assoc($Result))
  43.         {
  44.             //Alternate colors for rows
  45.             if($RowCt++ % 2 == 0) $Style = "background-color: #242425; color:#ffffff";
  46.             else $Style = "background-color: #000000; color:#ffffff";
  47.            
  48.             $Table.= "<tr style=\"$Style\">";
  49.             //Loop thru each field
  50.             foreach($Row as $field => $value)
  51.             {
  52.                 $Table.= "<td>$value</td>";
  53.             }
  54.             $Table.= "</tr>";
  55.         }
  56.         $Table.= "<tr style=\"background-color: #fe921f; color: #FFFFFF;\"><td colspan='$NumFields'>Query Returned " . mysqli_num_rows($Result) . " records</td></tr>";
  57.     }
  58.     $Table.= "</table>";
  59.    
  60.     return $Table;
  61. }
  62.  
  63. function mysqli_field_name($result, $field_offset)
  64. {
  65.     $properties = mysqli_fetch_field_direct($result, $field_offset);
  66.     return is_object($properties) ? $properties->name : null;
  67. }
  68.  ?>
Add Comment
Please, Sign In to add comment