User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,552 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,472 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 2118 | Replies: 5
Reply
Join Date: Oct 2007
Posts: 6
Reputation: daver40 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
daver40 daver40 is offline Offline
Newbie Poster

Question Creating a dynamic query using MSSQL and PHP

  #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:

from sqlmain.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form action="ms_sql.php" method="get" name="main">
<input name="name" type="text">
<input name="submit" type="submit" value="Submit">
</form>
</body>
</html>

from ms_sql.php

$host ='mssql.library.univ.edu';
$user = 'LIB_webuser';
$pass ='######';
$db =  'LIB';

//Open connection

$connection = mssql_connect($host, $user, $pass) or die("Unable to connect");

//Select database

mssql_select_db($db) or die("Unable to select database");

//Create query

//$query = "SELECT * FROM dbo.dbd";
$name = $_POST['name'];

$query = "SELECT $name FROM bdb";

// Execute query

$result = mssql_query($query);

while($row = mssql_fetch_array($result)){
	echo $row['netid'];
	echo "<br />";

//free result set memory

mssql_free_result($result);

//close connection

mssql_close($connection);

Thanks for the help!

David
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Location: Budapest
Posts: 252
Reputation: fatihpiristine has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 14
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: Creating a dynamic query using MSSQL and PHP

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

Re: Creating a dynamic query using MSSQL and PHP

  #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  
Join Date: Oct 2007
Posts: 6
Reputation: daver40 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
daver40 daver40 is offline Offline
Newbie Poster

Re: Creating a dynamic query using MSSQL and PHP

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



It works! Thanks.
Reply With Quote  
Join Date: Sep 2007
Location: Budapest
Posts: 252
Reputation: fatihpiristine has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 14
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: Creating a dynamic query using MSSQL and PHP

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

Help Re: Creating a dynamic query using MSSQL and PHP

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 5:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC