944,111 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 6022
  • PHP RSS
Oct 18th, 2007
0

Creating a dynamic query using MSSQL and PHP

Expand Post »
I'm a bit of a beginning to PHP, so I hope that this question isn't too terribly simple. I'm trying to write a flexible query that will use data from an HTML form to create a SELECT statement for a MSSQL database. I've tried writing a sample query, but I can't seem to get any data out of the database.

Here's my code:

PHP Syntax (Toggle Plain Text)
  1.  
  2. from sqlmain.html
  3.  
  4. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  8. <title>Untitled Document</title>
  9. </head>
  10.  
  11. <body>
  12. <form action="ms_sql.php" method="get" name="main">
  13. <input name="name" type="text">
  14. <input name="submit" type="submit" value="Submit">
  15. </form>
  16. </body>
  17. </html>
  18.  
  19. from ms_sql.php
  20.  
  21. $host ='mssql.library.univ.edu';
  22. $user = 'LIB_webuser';
  23. $pass ='######';
  24. $db = 'LIB';
  25.  
  26. //Open connection
  27.  
  28. $connection = mssql_connect($host, $user, $pass) or die("Unable to connect");
  29.  
  30. //Select database
  31.  
  32. mssql_select_db($db) or die("Unable to select database");
  33.  
  34. //Create query
  35.  
  36. //$query = "SELECT * FROM dbo.dbd";
  37. $name = $_POST['name'];
  38.  
  39. $query = "SELECT $name FROM bdb";
  40.  
  41. // Execute query
  42.  
  43. $result = mssql_query($query);
  44.  
  45. while($row = mssql_fetch_array($result)){
  46. echo $row['netid'];
  47. echo "<br />";
  48.  
  49. //free result set memory
  50.  
  51. mssql_free_result($result);
  52.  
  53. //close connection
  54.  
  55. mssql_close($connection);

Thanks for the help!

David
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daver40 is offline Offline
6 posts
since Oct 2007
Oct 18th, 2007
0

Re: Creating a dynamic query using MSSQL and PHP

try this:
$name = $_REQUEST['name'];
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Oct 19th, 2007
0

Re: Creating a dynamic query using MSSQL and PHP

Thanks for the help! I'm going to be testing out the code tomorrow, and I'll let you know if it works.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daver40 is offline Offline
6 posts
since Oct 2007
Oct 20th, 2007
0

Re: Creating a dynamic query using MSSQL and PHP

try this:
$name = $_REQUEST['name'];

It works! Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
daver40 is offline Offline
6 posts
since Oct 2007
Oct 20th, 2007
0

Re: Creating a dynamic query using MSSQL and PHP

welcome
Reputation Points: 6
Solved Threads: 19
Posting Whiz in Training
fatihpiristine is offline Offline
283 posts
since Sep 2007
Nov 30th, 2007
0

Re: Creating a dynamic query using MSSQL and PHP

Just an brief explanation...

Click to Expand / Collapse  Quote originally posted by daver40 ...
...
<form action="ms_sql.php" method="get" name="main">
...
$name = $_POST['name'];
...
You tried to read data from POST array, but you send your query thru GET array. If you want to do this, use $_REQUEST array as someone said
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mandragora is offline Offline
3 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Problem with creating scheduling program
Next Thread in PHP Forum Timeline: Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC