Page 1 of 1

PHP and MS SQL

Posted: Sun Oct 15, 2006 9:54 pm
by Pancoro
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Under Windows XP,
I try to connect PHP 5 to MS SQL 2000, this is the script:

Code: Select all

$server = "ipaddress:portnumber";  // the default is port 1433 for MS SQL, right?
$user = "username”;
$passwd = “password”;
$db_name = "dbanme”;
$table_name = "tablename”; // dbo.[table_name] or just [table_name] ?? Need specific path or something ?
$conn = mssql_connect($server, $user, $passwd) or die("Couldn't connect to SQL Server on $server"); 
mssql_select_db($db_name, $conn);
$query = "SELECT * FROM $table_name ";
$result = mssql_query( $query );

while ($data = mysql_fetch_array($result))
{
echo ("Date: ".$data["field_1"]."<br>");
echo ("Valuta : ".$data["field_2"]."<br>");
echo ("Value : ".$data["field_3"]."<br>");
}
Both IP target and my own PC use MS SQL Server, not client.
I enabled php.ini extension for php_mssql.dll, but just blank page that I get.
Please help me !


Thanks!! :)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Oct 16, 2006 9:31 am
by ryuuka
o/

if you are using adoDB then i find that this works very well:

this is the database connection:

Code: Select all

$objConn = new COM("adodb.connection");
  
$objConn->Open("dsn=Gispen_Inventory;server=sr0005.gispen.net;database=Gispen_ICT;Trusted_Connection=yes");
and this is a example of an SQL query:

the $objConn specifies wich database connection to use and execute makes sure it's going to be done(ADODB thing)

Code: Select all

$rs = $objConn->Execute("SELECT Printers, totaal, maandnr FROM gispen_printers_totaal WHERE (jaar = '$jaar')";
GL

Posted: Mon Oct 16, 2006 10:40 am
by RobertGonzalez
Turn on error reporting and display errors in your php.ini (for testing only, not for production).

Posted: Tue Oct 17, 2006 1:30 am
by ryuuka

Code: Select all

error_reporting(E_ALL); 
ini_set('display_errors', TRUE);
this piece of code gives errors even if it's specified that the site shouldn't do this
this helped me out numerous times.
might be of use to you

gl

Code error

Posted: Tue Oct 17, 2006 8:09 am
by churt
Looks like just a typo. Your while statement is using the mysql_fetch_array instead of the mssql_fetch_array.

Code: Select all

//error
while ($data = mysql_fetch_array($result)) 

//correction
while ($data = mssql_fetch_array($result))

Posted: Tue Oct 17, 2006 10:04 am
by RobertGonzalez
That'll do it (good catch by the way).

Take note also, that when using the error_reporting and display_errors ini directives that they will not report runtime errors (syntax errors) as those are evaluated prior to parsing. So if you are still getting blank pages after putting those two lines in, you have a syntax error somewhere in your code.