Advertisement
Guest User

Untitled

a guest
Mar 10th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. <html>
  2. <head>
  3. <title>PHP SQL Example</title>
  4. </head>
  5. <body>
  6. <h3> Query </h3>
  7.  
  8. <form action="" method="post">
  9.     Which table to query: <input type="text" name="table"> <br> <br>
  10.     <input type="submit" name="Submit"/>
  11. </form>
  12.  
  13. <?php
  14.     $server = "upsql";
  15.     $sqlUserName = "sqlbms6112";
  16.     $sqlPassword = "password";
  17.     $databaseName = "bms6112";
  18.    
  19.     $connectionInfo = array('Database'=>$databaseName, 'UID'=>$sqlUserName,
  20.         'PWD'=>$sqlPassword, 'Encrypt'=>'0', 'ReturnDatesAsStrings'=>true);
  21.        
  22.     $connection = sqlsrv_connect($server, $connectionInfo)
  23.         or die("Error: database perameters are not correct");
  24.     if(!empty($_POST["table"]))
  25.     {
  26.         // get the table name
  27.         $table = $_POST['table'];
  28.        
  29.         // prepare the SQL query
  30.         $query = "SELECT * FROM $table";
  31.         echo "Query: ".$query."<br>";
  32.         $result = sqlsrv_query($connection, $query)
  33.             or die("Error: query is wrong");
  34.            
  35.         echo "<table border=1>";
  36.         echo "<tr>";
  37.        
  38.         foreach(sqlsrv_field_metadata($result) as $fieldMetadata)
  39.             echo "<th>".$fieldMetadata['Name']."</th>";
  40.         echo "</tr>";
  41.        
  42.         // fetch rows in the table
  43.         while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
  44.         {
  45.             echo "<tr>\n";
  46.             foreach($row as $cell)
  47.             {
  48.                 echo "<td> $cell </td>";
  49.             }
  50.             echo "</tr>\n";
  51.         }
  52.         echo "</table>";
  53.     }
  54.     sqlsrv_close($connection);
  55.  
  56.  
  57.  
  58.  
  59. ?>
  60. </body>
  61. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement