954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating a dynamic query using MSSQL and PHP

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 "";

//free result set memory

mssql_free_result($result);

//close connection

mssql_close($connection);


Thanks for the help!

David

daver40
Newbie Poster
6 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 

Thanks for the help! I'm going to be testing out the code tomorrow, and I'll let you know if it works.

daver40
Newbie Poster
6 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
try this: $name = $_REQUEST['name'];


It works! Thanks.

daver40
Newbie Poster
6 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

welcome

fatihpiristine
Posting Whiz in Training
283 posts since Sep 2007
Reputation Points: 6
Solved Threads: 19
 

Just an brief explanation...

... ... $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

mandragora
Newbie Poster
3 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You