Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

odbc_connect not working in browser

Status
Not open for further replies.

peterworth

Programmer
Aug 18, 2004
80
GB
odbc_connect returns null when my script is run in a browser (firefox and IE), but the exact same code works perfectly when run with the command line PHP exe. does anyone know why this might happen?

thanks.
 
Your scripts are never run in a browser - PHP runs on the server, your webserver.
Is there a permission problem that the server can't connect?
Be aware that the script runs as the web server user, not you.
 
but wouldnt it be the same for the command line? also, is the user not specified by an argumaent in odbc_connect?
 
Provider=MSDASQL.1;Persist Security Info=False;Data Source=database1
 
You appear to be using the OLEDB provider for ODBC here. Can you use the native ODBC driver for the data base (or even a PHP library).
I'm curious to know how it worked in command line mode and not in server mode.
sorry to go back to basics can you show some actual PHP and the error you get.
I did a quick google and it suggests DSN=DataSourceName;UID=sa;PWD=Password;"
for a connection string
Can you also tell us what database your trying to get to


 
hmm, i dont know anything about OLEDB, i'm using MySQL and MyODBC drivers.

Code:
$db = odbc_connect('database1','','',SQL_CUR_USE_ODBC);
if (!$db)
{
	exit("Connection Failed");
}
$sql = "SELECT * FROM MAIN";
$result = odbc_exec($db,$sql);
if (!$result)
{
	exit("Query Failed");
}
odbc_fetch_into($result, $array);
$out = $array[0];
echo "test 2: $out\n";

The command line outputs the first field of the first row as it should, but the browser outputs "Connection failed" (from inside the if statement). Is there some way of connecting by actually writing the connection string manually?
 
The manual has this snippet
Code:
To open a DSN-less connection on Win32, you can use this:
$db_connection = new COM("ADODB.Connection");
$db_connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("../databases/database.mdb") . " ;DefaultDir=" . realpath("../databases");
$db_connection->open($db_connstr); 
$rs = $db_connection->execute("SELECT * FROM Table");
You will need to play around with it to get it to work with mysql, and note you are using the COM interface rather than ODBC.
The datasource you are using (database1) I suspect has been set up as a user data source rahter than a system one. The point being that a user based one is only accessable by the user who created (which may be different from the one that the web server uses), If it's a system oe it should be avaialble to all.
Check it out by runing ODBCAD32.
You were using OLEDB, you can tell as your connection string was saying provider (amongst other things).
What I'm curious to know is why do you want to use ODBC rather than the mysql functions.
 
thanks for your help.

i want to use odbc functions so that the code i'm writing can be used by other people for other servers (with any database).

how do i set it up as a system data source?
 
I go to a dos prompt (usually start->run tyoe cmd and enter)
type odbcad32 (or go to the control panel)
click the system dsn tab
click add
highlight the mysql driver
click finish
key in the appropriate details
click ok
Delete the local one (click on the
 
yep, i didnt realise there was a separate tab for system data sources - i've changed it from user to system and it works now, thanks very much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top