Page 1 of 1

PHP newbie - need to connect to a SQL Server database

Posted: Tue Jun 13, 2006 5:07 pm
by jej1216
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]


I'm a newbie, forgive me.

I am working on PHP to conenct to a SQL Server database and extract data from a Table.  

This is probably pretty simple, but not for me.

I am using some manuals to stumble along ("PHP, MySQL, and Apache" and "Teach Yourself PHP in 10 Minutes" by Sams).

From these books, I know I have to:

1) connect to the server
2) connect to the database
3) select the data from the table


1) instead of $db = mysql_connect ("X","X","X")   what should I use to connect the SQL Server?
2) again, instead of mysql_select_db?
3) instead of mysql_query?

Also, How can I verify that I've successfully connected?

I tried the following on a local flavor of MySQL just to get the correct code to connect/select:

Code: Select all

<?php
/* database.php
   This is a test PHP file
   By Joe Johnson
*/
echo "Database - MySQL<br>";

$conn = mysql_connect("localhost", "test", "test");
mysql_select_db("test",$conn);
$sql = "SELECT * from JoesTable";
$result = mysql_query($sql, $conn) or die(mysql_error());
echo $result;
?>
And get no messages in return - what do I need to add to the PHP to get feedback?

TIA,

jej1216


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: Tue Jun 13, 2006 5:23 pm
by nam797
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]


This is what I use

Code: Select all

///////////////////////////
db_connect.php
function DBConnect(){
	$dbuser = "username";
		$dbpassword = "password";
	$dbname = "databasename";
	$dbhost = "127.0.0.1";
	$link = mysql_connect($dbhost, $dbuser, $dbpassword);
	mysql_select_db($dbname, $link);
}

Then all you need to do 

include('db_connect.php');

$DBConnect();

$result = mysql_query("Your Query"):

if (!$result){
  mysql_error();
}else{
 it worked
}

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: Tue Jun 13, 2006 6:02 pm
by bdlang
Hmm, ok to both nam797 and jej1216 >

Please post PHP code within the PHP tags. Makes it ALOT easier to read your code, and spot potential problems. This should also be in the 'PHP code' forum.

jej1216 >

You cannot simply print the value of $result, it's a 'Resource type' value returned from the call to mysql_query(). You have to utilize one of the mysql_fetch_*() functions to retrieve the data. Otherwise, you can use a call to mysql_error() on your connection and database selection lines as well, make sure the connection is good.
Please see the PHP manual section on MySQL.

nam797 >

Your function doesn't return a value, i.e. $link has no scope outside the function. You should pass $link back to the script and allow the other mysql function calls to use it. Although it's fine as-is, you should also consider not trapping the connection parameters within it, doesn't make for very portable code, kind of defeats the reason to create a custom function. Food for thought.

Posted: Tue Jun 13, 2006 6:35 pm
by RobertGonzalez
To connect to a SQL Server database you will use either the mssql family of PHP DB functions or the ODBC family of PHP DB functions. If you are on PHP 5+ you can use ODBC with PDO as well I believe.

Posted: Tue Jun 13, 2006 7:20 pm
by bdlang
Everah wrote:To connect to a SQL Server database you will use either the mssql family of PHP DB functions or the ODBC family of PHP DB functions. If you are on PHP 5+ you can use ODBC with PDO as well I believe.
Valid point. I *ahem* assumed the user is using MySQL based on everything else in the post. But you never know. :/

Re: PHP newbie - need to connect to a SQL Server database

Posted: Tue Jun 13, 2006 11:57 pm
by RobertGonzalez
From the original post...
jej1216 wrote:I'm a newbie, forgive me.

I am working on PHP to conenct to a SQL Server database and extract data from a Table.

Thanks

Posted: Wed Jun 14, 2006 9:24 am
by jej1216
Point taken on the method of posting code - my apologies.

And, thanks for the info on using ODBC andf on my code errors. I was trying to mimic the sample code in the books I referenced - obviously they were not very complete.

I do have one question - "I don't offer solutions via PM. It is much more helpful to the community if we can solve problems in the open so others can benefit from the solution as well." was one of the replies ---- huh?!?

I am not familiar with PM and if I posted incorrectly please let me know - I thought I was asking for assistance in the open.

Anyway, thanks for the posts - they were very helpful.

jej1216

Posted: Wed Jun 14, 2006 9:28 am
by RobertGonzalez
jej1216 wrote:I do have one question - "I don't offer solutions via PM. It is much more helpful to the community if we can solve problems in the open so others can benefit from the solution as well." was one of the replies ---- huh?!?

I am not familiar with PM and if I posted incorrectly please let me know - I thought I was asking for assistance in the open.
You did what you were supposed to do (save for the code posting issue :wink: ). The part you quoted wasn't a reply. It is part of my signature (see the bottom of this post and all of my posts). It is not a response to you, but a general notice to everyone.

Got It

Posted: Wed Jun 14, 2006 5:28 pm
by jej1216
Ah, got it.

Also, I am using MySQL on my local machine for practice but will have to connect to SQL Server on a DB Server down the road. That's why my questin asked about SQL Server but my sample code was MySQL.

Thanks,

jej1216