Advertisement
Guest User

Untitled

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