Page 1 of 1

Issues with MSSQL

Posted: Fri Dec 08, 2006 5:31 pm
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.

Posted: Sat Dec 09, 2006 2:00 am
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>

Posted: Mon Dec 11, 2006 1:04 pm
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

Posted: Mon Dec 11, 2006 1:36 pm
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 :( ?