ODBC Connection to SQL Server and PHP

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
Mia
Forum Newbie
Posts: 1
Joined: Mon Jul 29, 2002 5:17 pm

ODBC Connection to SQL Server and PHP

Post by Mia »

I created a System ODBC DSN, and I tested the connection through ODBC wizard and it seems fine, but when I try to connect to the database using PHP script, I get the following error:

Couldnt connect to the datasource.
Specified driver could not be loaded due to system error 5 (SQL Server).

I know the sql server account (username and password) has proper permissions, because I connect through JDBC using Java just fine.

I tried multiple functions, none of them works for example this is one of them:
<?php
$connection = odbc_connect('PHPdb','myuser','connectnow') or die ('Couldnt connect to the datasource.<br />'.odbc_errormsg());

// create SQL statement
$sql = "SELECT * FROM phpTest";

$sql_statement = odbc_prepare($connection, $sql) or die ('Couldnt prepare statement.<br />'.odbc_errormsg());
$sql_result = odbc_execute($sql_statement) or die ('Couldnt execute query.<br />'.odbc_errormsg());
odbc_result_all($sql_result, 'border=1') or die(odbc_errormsg());
odbc_free_result($sql_result) or die(odbc_errormsg());
odbc_close($connection);
?>

Any ideas?

Mia
johnbran
Forum Newbie
Posts: 17
Joined: Tue Jul 16, 2002 8:18 pm

Post by johnbran »

Mia I'm a newbie.....I was just given a project and told to make it happen using PHP....I've been having such a hard time. But thanks to the internet, php books, and this board I've been able to muddle my way through.

However, one area where I didn't have trouble was getting my odbc connection to SQL server to work properly. I use the following and it works beautifully everytime. If you have questions please let me know.

Code: Select all

<?php

$odbc_dsn="EmpDir";
$odbc_userid="Guest";
$odbc_password="";
$user_tablename='rates_output';

if(!($odbc_db=odbc_connect($odbc_dsn, $odbc_userid, $odbc_password))) 
	die ("Could not connect to ODBC data source $odbc_dsn. Please re-try");

	$query="Select * from $user_tablename";

if(!($odbc_rs=odbc_do($odbc_db, $query)))
	die ("Error executing query $query");
Hope this helps!
Geschi
Forum Newbie
Posts: 21
Joined: Wed Jul 10, 2002 3:21 am
Location: Germany

What about the ODBC driver

Post by Geschi »

Have you considered that maybe your ODBC driver settings are not referred to correctly?

Here is a sample connection from http://www.iodbc.org; just paste your settings and give it a try:

<?php
putenv("LD_LIBRARY_PATH=/usr/local/src/odbcsdk/lib");
putenv("ODBCINSTINI=/path/to/odbcinst.ini"); //this location will be determined by your driver install.
putenv("ODBCINI=/path/to/odbc.ini"); //odbc.ini contains your DSNs, location determined by your driver install.
$dsn="DSN_NAME"; // this is a valid DSN‚ can be tested in odbctest
$user="username";
$password="password";

$sql="SELECT * FROM table"; //replace “table” with a table name
if ($conn_id=odbc_connect("$dsn","","")){
echo "connected to DSN: $dsn";
if($result=odbc_do($conn_id, $sql)) {
echo "executing '$sql'";
echo "Results: ";
odbc_result_all($result);
echo "freeing result";
odbc_free_result($result);
}
else{
echo "can not execute '$sql' ";
}
echo "closing connection $conn_id";
odbc_close($conn_id);
}
else{
echo "can not connect to DSN: $dsn ";
}
?>
Post Reply