Connect to database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Connect to database

Post by leewad »

Is it possible to connect to a database located on another server ?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

Yes. There is a server parameter which is localhost normally for php scripts connecting to db on the same machine.
Instead of localhost supply the IP address.
$con=mysql_connect("xxx.xxx.xxx.xxx",$dbUsername,$dbPassword);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

...however, most databases are configured for local access only, and for good reason.
Ashabbir
Forum Newbie
Posts: 6
Joined: Mon Nov 01, 2004 2:47 pm
Location: UK

Oracle

Post by Ashabbir »

hye

Dont suppose you know how to connect to racle from php, using oracle 9i. Don't know the syntax as this is my first time using PHP.
User avatar
cto1mac
Forum Commoner
Posts: 54
Joined: Tue Jan 27, 2004 6:11 am
Location: Virginia Beach, VA

Post by cto1mac »

I agree with feyd on this, most db's are configured to allow local access only. But, if you want to connect to an Oracle9i server there are two parts. 1. Did you install php with oracle support?
--> You can do this two ways. Either:
-->-->a. uncomment these lines from your php.ini file:

Code: Select all

;extension = php_oci8.dll
;extension = php_oracle.dll
or:
-->-->b. recompile php with the following options:

Code: Select all

--with-oracle=/path/to/oracle/home/dir
--with-oci8=/path/to/oracle/home/dir
Note: you can check weather or not oracle is enabled within php with a simple php file with:

Code: Select all

<?php 
phpinfo(); 
?>
When you call that page from your browser it will give you all sorts of information about your php install. Make sure to delete this file or move it after you are done.

The actual connection to the db is accomplished with the following:
--This is using the OCI Extension Module --

Code: Select all

<?php

if ($c=OCILogon("scott", "tiger", "orcl")) {
  echo "Successfully connected to Oracle.\n";
  OCILogoff($c);
} else {
  $err = OCIError();
  echo "Oracle Connect Error " . $err[text];
}
?>
or using the ORA Extension module:

Code: Select all

<?php

if ($c=ora_logon("scott@orcl","tiger")) {
  echo "Successfully connected to Oracle.\n";
  ora_commitoff($c);
  ora_logoff($c);
} else {
  echo "Oracle Connect Error " . ora_error();
}
?>
here is an example for select, insert, update and delete:

Code: Select all

<?php
$c=OCILogon("scott", "tiger", "orcl");
  if ( ! $c ) {
    echo "Unable to connect: " . var_dump( OCIError() );
    die();
  }

  // Drop old table...
  $s = OCIParse($c, "drop table tab1");
  OCIExecute($s, OCI_DEFAULT);

  // Create new table...
  $s = OCIParse($c, "create table tab1 (col1 number, col2 varchar2(30))");
  OCIExecute($s, OCI_DEFAULT);

  // Insert data into table...
  $s = OCIParse($c, "insert into tab1 values (1, 'Frank')");
  OCIExecute($s, OCI_DEFAULT);

  // Insert data using bind variables...
  $var1 = 2;
  $var2 = "Scott";
  $s = OCIParse($c, "insert into tab1 values (:bind1, :bind2)");
  OCIBindByName($s, ":bind1", $var1);
  OCIBindByName($s, ":bind2", $var2);
  OCIExecute($s, OCI_DEFAULT);

  // Select Data...
  $s = OCIParse($c, "select * from tab1");
  OCIExecute($s, OCI_DEFAULT);
  while (OCIFetch($s)) {
    echo "COL1=" . ociresult($s, "COL1") .
       ", COL2=" . ociresult($s, "COL2") . "\n";
  }

  // Commit to save changes...
  OCICommit($c);

  // Logoff from Oracle...
  OCILogoff($c);
?>
I hope this helps. In either event you came to the right place for asking questions.
Ashabbir
Forum Newbie
Posts: 6
Joined: Mon Nov 01, 2004 2:47 pm
Location: UK

Re Oracle Connection

Post by Ashabbir »

Hye Thanks for that, Much appreciated but I have now decided to use MYSQL and php which I have connected
Shab
Post Reply