Creating a dynamic query using MSSQL and PHP

Reply

Join Date: Oct 2007
Posts: 6
Reputation: daver40 is an unknown quantity at this point 
Solved Threads: 0
daver40 daver40 is offline Offline
Newbie Poster

Creating a dynamic query using MSSQL and PHP

 
0
  #1
Oct 18th, 2007
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 271
Reputation: fatihpiristine has a little shameless behaviour in the past 
Solved Threads: 16
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: Creating a dynamic query using MSSQL and PHP

 
0
  #2
Oct 18th, 2007
try this:
$name = $_REQUEST['name'];
Do a favour, leave me alone
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: daver40 is an unknown quantity at this point 
Solved Threads: 0
daver40 daver40 is offline Offline
Newbie Poster

Re: Creating a dynamic query using MSSQL and PHP

 
0
  #3
Oct 19th, 2007
Thanks for the help! I'm going to be testing out the code tomorrow, and I'll let you know if it works.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: daver40 is an unknown quantity at this point 
Solved Threads: 0
daver40 daver40 is offline Offline
Newbie Poster

Re: Creating a dynamic query using MSSQL and PHP

 
0
  #4
Oct 20th, 2007
Originally Posted by fatihpiristine View Post
try this:
$name = $_REQUEST['name'];

It works! Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 271
Reputation: fatihpiristine has a little shameless behaviour in the past 
Solved Threads: 16
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: Creating a dynamic query using MSSQL and PHP

 
0
  #5
Oct 20th, 2007
welcome
Do a favour, leave me alone
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3
Reputation: mandragora is an unknown quantity at this point 
Solved Threads: 0
mandragora's Avatar
mandragora mandragora is offline Offline
Newbie Poster

Re: Creating a dynamic query using MSSQL and PHP

 
0
  #6
Nov 30th, 2007
Just an brief explanation...

Originally Posted by daver40 View Post
...
<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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC