Issues with MSSQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jimmeye
Forum Newbie
Posts: 3
Joined: Fri Dec 08, 2006 5:15 pm

Issues with MSSQL

Post by jimmeye »

Hey guys...

I'm new to the forum but not new to PHP. I am, however, trying something I haven't done before, and that is to make a connection to MSSQL. I've done plenty of work with PHP and MySQL, so I'm not illiterate... :)

Here's some code I'm using to make my connection...

Code: Select all

<html>
<head>
</head>
<body>
<?php
	echo"hello";
	$query = "exec GetLastMonthsLeads";
	
	MSSQL_CONNECT('dbdev','kleadreaderOAS','******')or die("Couldn't connect to the database!");
	mssql_select_db('consumer')or die("Couldn't select to the database!");	
	$result = MSSQL_QUERY($query)or die("Couldn't query to the database!");

	$resultamnt = MSSQL_NUM_ROWS($result)or die("Couldn't num the database!");
	for($i=0;$i<$resultamnt;$i++){
		$line = mssql_fetch_array($result);
		echo $line[$i];
	}
?>
	</body>
</html>
The "GetLastMonthsLeads" is a stored proc on dbdev/consumer.
I'm using PHP 4.something and this is on a W2K server. With the code above when I run it and look at the source this is what I get...

Code: Select all

<html>
<head>
</head>
<body>
hello
Which is telling me something is very wrong with my PHP install because it's not allowing all the html to go to source. If I move the echo"hello" line to another location after the first MSS command, it doesn't even echo.

OK... with all that in mind, could there be something missing with my php install? I do have the ODBC set up, for those who would ask. It seems to me that the password, username can't be the issue because even the DIE string isn't working. I've searched all over and am not finding much clarity on this issue. Any help would be appreciated.

Thanks.
Last edited by jimmeye on Mon Dec 11, 2006 1:37 pm, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<html>
	<head>
	</head>
	<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$db = mssql_connect('dbdev','kleadreaderOAS','&TAU/h6A')or die("Couldn't connect to the database! ".mssql_get_last_message());
mssql_select_db('consumer', $db)or die("Couldn't select to the database! ".mssql_get_last_message()); 
$query = "exec GetLastMonthsLeads";
echo '<div>', $query, "</div>\n"; flush();
$result = mssql_query($query, $db)or die("Couldn't query to the database!".mssql_get_last_message());

$resultamnt = mssql_num_rows($result);
echo '<div>', $resultamnt, " records</div>\n"; flush();
for($i=0; $i<$resultamnt; $i++) {
	$line = mssql_fetch_array($result);
	echo $line[$i];
}
?>
	</body>
</html>
jimmeye
Forum Newbie
Posts: 3
Joined: Fri Dec 08, 2006 5:15 pm

Post by jimmeye »

Thanks volka... that helps with the trouble shooting, and in fact it produced this line...

Fatal error: Call to undefined function: mssql_connect()

This must indicate that I need a module of some sort. Where can I find that?

Jim
jimmeye
Forum Newbie
Posts: 3
Joined: Fri Dec 08, 2006 5:15 pm

Post by jimmeye »

After doing a web search on that error term I came up with this code...

Code: Select all

<?php 
error_reporting(E_ALL); 
ini_set('display_errors', true); 

$sqlconnect=odbc_connect('DBPROD','leadreaderOAS','******')or die("Couldn't connect to the database! "); 
mssql_select_db('consumer', $db)or die("Couldn't select to the database! "); 
$query = "exec GetLastMonthsLeads"; 
echo '<div>', $query, "</div>\n"; flush(); 
$result = odbc_exec($sqlconnect, $query)or die("Couldn't query to the database!"); 

$resultamnt = mssql_num_rows($result); 
echo '<div>', $resultamnt, " records</div>\n"; flush(); 
for($i=0; $i<$resultamnt; $i++) { 
        $line = mssql_fetch_array($result); 
        echo $line[$i]; 
} 
?>
... which uses the ODBC instead of a direct connect. I got it to work except that I'm on a different side of the fire wall from the DB. Which brought me to another issue...

The app I'm trying to replace is a three teir java app. Is this kind of structure possible with PHP or do I have to dive in to ASP.net :( ?
Post Reply