文字

odbc_connect

(PHP 4, PHP 5)

odbc_connectConnect to a datasource

说明

resource odbc_connect ( string $dsn , string $user , string $password [, int $cursor_type ] )

The connection id returned by this functions is needed by other ODBC functions. You can have multiple connections open at once as long as they either use different db or different credentials.

With some ODBC drivers, executing a complex stored procedure may fail with an error similar to: "Cannot open a cursor on a stored procedure that has anything other than a single select statement in it". Using SQL_CUR_USE_ODBC may avoid that error. Also, some drivers don't support the optional row_number parameter in odbc_fetch_row() . SQL_CUR_USE_ODBC might help in that case, too.

参数

dsn

The database source name for the connection. Alternatively, a DSN-less connection string can be used.

user

The username.

password

The password.

cursor_type

This sets the type of cursor to be used for this connection. This parameter is not normally needed, but can be useful for working around problems with some ODBC drivers.

The following constants are defined for cursortype:

  • SQL_CUR_USE_IF_NEEDED
  • SQL_CUR_USE_ODBC
  • SQL_CUR_USE_DRIVER

返回值

Returns an ODBC connection or ( FALSE ) on error.

范例

Example #1 DSN-less connections

<?php
// Microsoft SQL Server using the SQL Native Client 10.0 ODBC Driver - allows connection to SQL 7, 2000, 2005 and 2008
$connection  odbc_connect ( "Driver={SQL Server Native Client 10.0};Server= $server ;Database= $database ;" $user $password );

// Microsoft Access
$connection  odbc_connect ( "Driver={Microsoft Access Driver (*.mdb)};Dbq= $mdbFilename " $user $password );

// Microsoft Excel
$excelFile  realpath ( 'C:/ExcelData.xls' );
$excelDir  dirname ( $excelFile );
$connection  odbc_connect ( "Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq= $excelFile ;DefaultDir= $excelDir '' '' );
?>

参见

  • For persistent connections: odbc_pconnect() - Open a persistent database connection

用户评论:

[#1] Anonymous [2015-09-19 18:33:45]

how to solve Error SQL state S1009 connecting to an access database in a network with odbc

1 - if you dont have access installed in your computer, you will need the access driver for odbc conection...install with mdactypex (just for 32 bits)... or AccessDatabaseEngine_X64.exe for 64bits

2 - give needed rights to a windows user profile to run as a service: Control panel, Administrative tools, in the left treeview panel, select Local Security Policies, User Rights Assignment, and in the right panel find and select 'Logon as a service', right button click and properties-> Search and add the user User in your computer you want to Apache use to login at service start

3 - Go to Windows Services admin, select Apache, edit properties, select login tab, and set the previuos user you use (write his windows login password) 

4- Use php code like this:
$user="your_db_user";
$password="your_db_password";

 // Conexion Access con ODBC
// Important: (4 initial slashes!) \\\\network_path\dir_name\...
$mdbFilename='\\\\servidor\data\AccessBD.mdb';

$conn_access = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=$mdbFilename", $user, $password);

[#2] Matt [2015-09-03 15:00:43]

In newer versions you may need: odbc_connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:/Folder/database.mdb", "", "");

[#3] cjbauer2 at hotmail dot com [2015-05-21 22:47:16]

<?php
//create an instance of the ADO connection object
$conn = new COM ("ADODB.Connection") or die("Cannot start ADO");

//define connection string, specify database driver
$connStr "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source= c:\inetpub\wwwroot\db\examples.mdb";
  
$conn->open($connStr); //Open the connection to the database

//declare the SQL statement that will query the database
$query "SELECT * FROM cars";

//execute the SQL statement and return records
$rs $conn->execute($query);

$num_columns $rs->Fields->Count();
echo 
$num_columns "<br />"

for (
$i=0$i $num_columns$i++) {
    
$fld[$i] = $rs->Fields($i);
}
echo 
"<table>";
while (!
$rs->EOF)  //carry on looping through while there are records
{
    echo 
"<tr>";
    for (
$i=0$i $num_columns$i++) {
        echo 
"<td>" $fld[$i]->value "</td>";
    }
    echo 
"</tr>";
    
$rs->MoveNext(); //move on to the next record
}
echo 
"</table>";
//close the connection and recordset objects freeing up resources 
$rs->Close();
$conn->Close();
$rs null;
$conn null;
?>

[#4] cjbauer2 at hotmail dot com [2015-05-21 20:52:16]


}
$rs->Close();
$db_connection->Close();
?>

(Prints first 2 columns for each row.)

[#58] cs at coolspot dot de [2001-07-10 13:01:16]

We've tried hard to connect from php to our IBM DB2 RS/6000 Server. It worked after we compiled with --ibm-db2= option, but it was unbelievable
slow.

No, just testing some options, we found out that it went from very slow (getting 100 records lasts 1 till 10 seconds) to fast access (almost same speed as with using JDBC from Servlets) to 0.2 till 0.3 seconds.

We simply added the optional parameter Cursortype to odbc_connect, and with the cursortype SQL_CUR_USE_ODBC it changed in that way!

Hope this helps anybody who must connect to db2 ;)

[#59] ckelly at powerup dot com dot au [2001-06-12 21:42:49]

To connect to a PROGRESS database using ODBC you must have SQL_CUR_USE_ODBC  as the 4th parameter eg odbc_connect(DSN,uname,password,SQL_CUR_USE_ODBC ) otherwise you can pass queries but no results are ever returned .

[#60] cpoirier at shelluser dot net [2001-03-09 13:26:05]

After much testing, and I think supported by a comment I found in the code, I have come to a disturbing conclusion: odbc_connect() in PHP4.04pl1 is really an odbc_pconnect(), with all the implications for transaction scoping.  Specifically, each time you call odbc_connect( "X", "" "" ), you will get the same physical ODBC Connection, and odbc_commit() and odbc_rollback() will affect all copies.  The only solution I could find was to use several different DSNs to access the database.

[#61] fc99 at smm dot de [2001-01-26 18:23:56]

If you don't want to specify your login credentials on your web server, you can leave the login fields blank to use the integrated windows security like here:

odbc_connect("DSN=DataSource","",""); 

Make sure you have switched your system dsn to integrated security, too !

(works on windows machines only, of course)

flo.

[#62] SilencerX at optidynamic dot com [2001-01-26 15:31:52]

If like me you are using openlink from unix to access an MS Access database on an NT/Win2k machine and find out that your INSERT queries don't do anything and don't report any errors, use odbc_pconnect().

I couldn't understand what was going on and after a bit of research I found out that with MySQL they recommended using mysql_pconnect() for INSERT queries. I tried the same thing with odbc and it worked.

[#63] garretg at otable dot com [2001-01-02 14:03:49]

If you're connecting to a SQL server database through ODBC, you must set the default database of the ODBC DSN to the database you want to use.

There is no way to specify the database name in odbc_connect or odbc_pconnect, just the DSN name, username, and password.

[#64] phobo_AT_paradise.net.nz [2000-11-02 06:01:05]

If using Openlink to connect to a Microsoft Access database, you will most likely fine tha odbd_connect() works fine, but discover that ANY query will produce odd results; with SELECT queries failing with "[OpenLink][ODBC][Driver]Driver not capable, SQL state
S1C00 in SQLExecDirect in xxxx.php on line xx" and INSERT / DELETE queries warning "No tuples available at this result index".

In this case, use the SQL_CUR_USE_ODBC cursor!

This had me stumped for quite some time; because it was the odbc_exec() which was seemingly at fault... :)

Siggy

[#65] cnewbill at onewest dot net [2000-05-18 05:55:29]

Alot of people share the same kind of problems getting this setup on linux.  I was assigned this problem 2 days ago and I was successful.  My combination was PHP4 RC2, Easysoft OOB, and unixODBC.  These three products work very well together and are real easy to install.  More info http://www.easysoft.com/products/oob/main.phtml. ps also works good with Perl's DBI.

上一篇: 下一篇: