Forum.: | PortaL |:.GalleryPytësoriKërkoLista AnëtarëveGrupet e AnëtarëveRegjistrohuidentifikimi

Share | 

 Examples of Connecting to Databases

Shko poshtë 

Numri i postimeve : 526
Age : 24
Vendi : kosovë
Registration date : 26/09/2008

MesazhTitulli: Examples of Connecting to Databases   Wed Oct 22, 2008 12:26 am

MySQL and Most Other Database Drivers

MySQL connections are very straightforward, and the parameters are identical
to mysql_connect:
$conn = &ADONewConnection('mysql');

# or dsn
$dsn = 'mysql://user:pwd@localhost/mydb';
$conn = ADONewConnection($dsn); # no need for Connect()

# or persistent dsn
$dsn = 'mysql://user:pwd@localhost/mydb?persist';
$conn = ADONewConnection($dsn); # no need for PConnect()

# a more complex example:
$pwd = urlencode($pwd);
$dsn = "mysql://user:$pwd@localhost/mydb?persist&clientflags=$flags";
$conn = ADONewConnection($dsn); # no need for PConnect()

For most drivers, you can use the standard function: Connect($server, $user, $password, $database), or
a DSN since ADOdb 4.51. Exceptions to this are listed below.


PDO, which only works with PHP5, accepts a driver specific connection string:
$conn =& NewADOConnection('pdo');

The DSN mechanism is also supported:
$conn =& NewADOConnection("pdo_mysql://user:pwd@localhost/mydb?persist"); # persist is optional


PostgreSQL 7 and 8 accepts connections using:
a. the standard connection string:
$conn = &ADONewConnection('postgres');
$conn->PConnect('host=localhost port=5432 dbname=mary');
b. the classical 4 parameters:

c. dsn:
$dsn = 'postgres://user:pwd@localhost/mydb?persist'; # persist is optional
$conn = ADONewConnection($dsn); # no need for Connect/PConnect


Here is an example of querying a LDAP server. Thanks to Josh Eldridge for the driver and this example:

/* Make sure to set this BEFORE calling Connect() */
$host = '';
$ldapbase = 'ou=People,o=Baylor University,c=US';

$ldap = NewADOConnection( 'ldap' );
$ldap->Connect( $host, $user_name='', $password='', $ldapbase );

echo "<pre>";

print_r( $ldap->ServerInfo() );
$userName = 'eldridge';

$rs = $ldap->Execute( $filter );
if ($rs)
while ($arr = $rs->FetchRow()) {

$rs = $ldap->Execute( $filter );
if ($rs)
while (!$rs->EOF) {

print_r( $ldap->GetArray( $filter ) );
print_r( $ldap->GetRow( $filter ) );

echo "</pre>";

Using DSN:
$dsn = "ldap://,o=Baylor University,c=US";
$db = NewADOConnection($dsn);


You define the database in the $host parameter:
$conn = &ADONewConnection('ibase');

Or dsn:
$dsn = 'firebird://user:pwd@localhost/mydb?persist&dialect=3'; # persist is optional
$conn = ADONewConnection($dsn); # no need for Connect/PConnect


Sqlite will create the database file if it does not exist.
$conn = &ADONewConnection('sqlite');
$conn->PConnect('c:\path\to\sqlite.db'); # sqlite will create if does not exist

Or dsn:
$path = urlencode('c:\path\to\sqlite.db');
$dsn = "sqlite://$path/?persist"; # persist is optional
$conn = ADONewConnection($dsn); # no need for Connect/PConnect

Oracle (oci8)

With oci8, you can connect in multiple ways. Note that oci8 works fine with
newer versions of the Oracle, eg. 9i and 10g.
a. PHP and Oracle reside on the same machine, use default SID.
$conn->Connect(false, 'scott', 'tiger');
b. TNS Name defined in tnsnames.ora (or ONAMES or HOSTNAMES), eg. 'myTNS'
$conn->PConnect(false, 'scott', 'tiger', 'myTNS');
$conn->PConnect('myTNS', 'scott', 'tiger');
c. Host Address and SID
# with adodb 5.06 or 4.991 and later
$conn->Connect('', 'scott', 'tiger', "SID=$SID");

# OR with all versions of ADOdb
$conn->connectSID = true;
$conn->Connect('', 'scott', 'tiger', $SID);

d. Host Address and Service Name
$conn->Connect('', 'scott', 'tiger', 'servicename');
e. Oracle connection string:
$conn->Connect($cstr, 'scott', 'tiger');

f. ADOdb dsn:
$dsn = 'oci8://user:pwd@tnsname/?persist'; # persist is optional
$conn = ADONewConnection($dsn); # no need for Connect/PConnect

$dsn = 'oci8://user:pwd@host/sid';
$conn = ADONewConnection($dsn);

$dsn = 'oci8://user:pwd@/'; # oracle on local machine
$conn = ADONewConnection($dsn);

You can also set the charSet for Oracle 9.2 and later, supported since PHP 4.3.2, ADOdb 4.54:
$conn->charSet = 'we8iso8859p1';

# or
$dsn = 'oci8://user:pwd@tnsname/?charset=WE8MSWIN1252';
$db = ADONewConnection($dsn);

DSN-less ODBC (Access, MSSQL and DB2 examples)

ODBC DSN's can be created in the ODBC control panel, or you can use a DSN-less
connection.To use DSN-less connections with ODBC you need PHP 4.3 or later.

For Microsoft Access:
$db =& ADONewConnection('access');
$dsn = "Driver={Microsoft Access Driver (*.mdb)};Dbq=d:\\northwind.mdb;Uid=Admin;Pwd=;";

For Microsoft SQL Server:
$db =& ADONewConnection('odbc_mssql');
$dsn = "Driver={SQL Server};Server=localhost;Database=northwind;";

or if you prefer to use the mssql extension (which is limited to mssql 6.5 functionality):
$db =& ADONewConnection('mssql');
$db->Execute("localhost', 'userid', 'password', 'northwind');

For DB2:
$db =& ADONewConnection('db2');
$dsn = "driver={IBM db2 odbc DRIVER};Database=sample;hostname=localhost;port=50000;protocol=TCPIP;".
"uid=root; pwd=secret";

DSN-less Connections with ADO
If you are using versions of PHP earlier than PHP 4.3.0, DSN-less connections
only work with Microsoft's ADO, which is Microsoft's COM based API. An example
using the ADOdb library and Microsoft's ADO:
$db = &ADONewConnection("ado_mssql");
print "<h1>Connecting DSN-less $db->databaseType...</h1>";

. "SERVER=flipper;DATABASE=ai;UID=sa;PWD=;" ;


$rs = $db->Execute("select * from table");
$arr = $rs->GetArray();
Mbrapsht në krye Shko poshtë
Shiko profilin e anëtarit
Examples of Connecting to Databases
Mbrapsht në krye 
Faqja 1 e 1

Drejtat e ktij Forumit:Ju nuk mund ti përgjigjeni temave të këtij forumi
 :: PC & Console :: Tutorials/Mesime-
Kërce tek: