Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>PHP SQL Example</title>
- </head>
- <body>
- <h3> Query </h3>
- <form action="" method="post">
- Which table to query: <input type="text" name="table"> <br> <br>
- <input type="submit" name="Submit"/>
- </form>
- <?php
- $server = "upsql";
- $sqlUserName = "sqlbms6112";
- $sqlPassword = "password";
- $databaseName = "bms6112";
- $connectionInfo = array('Database'=>$databaseName, 'UID'=>$sqlUserName,
- 'PWD'=>$sqlPassword, 'Encrypt'=>'0', 'ReturnDatesAsStrings'=>true);
- $connection = sqlsrv_connect($server, $connectionInfo)
- or die("Error: database perameters are not correct");
- if(!empty($_POST["table"]))
- {
- // get the table name
- $table = $_POST['table'];
- // prepare the SQL query
- $query = "SELECT * FROM $table";
- echo "Query: ".$query."<br>";
- $result = sqlsrv_query($connection, $query)
- or die("Error: query is wrong");
- echo "<table border=1>";
- echo "<tr>";
- foreach(sqlsrv_field_metadata($result) as $fieldMetadata)
- echo "<th>".$fieldMetadata['Name']."</th>";
- echo "</tr>";
- // fetch rows in the table
- while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC))
- {
- echo "<tr>\n";
- foreach($row as $cell)
- {
- echo "<td> $cell </td>";
- }
- echo "</tr>\n";
- }
- echo "</table>";
- }
- sqlsrv_close($connection);
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement