PHP and MS SQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pancoro
Forum Newbie
Posts: 4
Joined: Fri Oct 06, 2006 6:06 am

PHP and MS SQL

Post 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]
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post 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
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Turn on error reporting and display errors in your php.ini (for testing only, not for production).
ryuuka
Forum Contributor
Posts: 128
Joined: Tue Sep 05, 2006 8:18 am
Location: the netherlands

Post 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
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Code error

Post 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))
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply