Foros del Web » Programando para Internet » PHP »

Conexion Php general a cualquier bd

Estas en el tema de Conexion Php general a cualquier bd en el foro de PHP en Foros del Web. buen dia de antemano gracias por sua tencion y ayuda tengo una aplicacion hecha en php+mysql, pero me gustaria que esta aplicacion se pudiera conectar ...
  #1 (permalink)  
Antiguo 26/05/2007, 07:57
 
Fecha de Ingreso: marzo-2003
Mensajes: 164
Antigüedad: 21 años, 1 mes
Puntos: 0
Conexion Php general a cualquier bd

buen dia de antemano gracias por sua tencion y ayuda tengo una aplicacion hecha en php+mysql, pero me gustaria que esta aplicacion se pudiera conectar a cualquier base de datos oracle sqlserver en fin, cual seria la libreria mas adecuada para poder conectarme a cualquier base de datos, o tacaria utilizar odcb gracias por sua tencion y ayuda.
  #2 (permalink)  
Antiguo 26/05/2007, 08:37
(Desactivado)
 
Fecha de Ingreso: diciembre-2006
Mensajes: 529
Antigüedad: 17 años, 5 meses
Puntos: 11
Re: Conexion Php general a cualquier bd

Hola, visita Hotscripts.com hay muchas clases de ese estilo.
Hace algún tiempo descargue una que sirve para mysql y acces, a pesar de que yo no la uso (pues programé una de acuerdo a mis necesidades) esta buenba para que veas como hacerla, sólo debes agregarle a esta clase las bases de datos que faltan o que te gustarían utilizar.

Código PHP:
<?php
/*
Since every database has differences with queries, the following set of standardizations
has been created to make the queries work without modification between database types
[ and ] encapsulate field and table names (Access SQL style)
\' escapes a single quote (MySQL style), no other \ escape sequences should be used
Example:
INSERT INTO [Table] ([Field]) VALUES('It\'s a value')
*/
// set to 1 to enable a flushed print of any query run, 0 disables
define("DEBUG"0);
// the type of database to use:
define("ACCESS"0);
define("MYSQL"1);
class 
database {
 var 
$type=-1;
 var 
$conn='';
 var 
$results=array();
 var 
$index=array();
 var 
$count=array();
 
/*
  Constructor- establishes the database type and creates a connection
  @param: $type- the database type: ACCESS, MYSQL
  @param: $database- the database, for ACCESS, the path to the database file,
                     for MYSQL, the name of the database
  @param: $host- MYSQL only, the host where the database resides
  @param: $username- MYSQL only, the username for the database user
  @param: $password- MYSQL only, the password for username
 */
 
function database($type$database$host=''$username=''$password='') {
  
$this->type=$type;
  switch(
$this->type) {
  case 
ACCESS:
   
$this->conn=new COM('ADODB.Connection');
   
$this->conn->Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={$database};Mode=ReadWrite;");
   break;
  case 
MYSQL:
   
$this->conn=@mysql_connect($host,$username,$password);
   @
mysql_select_db($database$this->conn);
   break;
  }
 }
 
/*
  Query- runs a query on the database connection and saves the results for extraction
  @param: $sql- the SQL string
  @param: $key- an identifier to store the results
 */
 
function query($sql$key=0) {
  
$sql=$this->parse($sql);
  
$this->debug($sql);
  switch(
$this->type) {
  case 
ACCESS:
   
$this->results[$key]=$this->conn->Execute($sql);
   
$this->count[$key]=$this->results[$key]->RecordCount();
   break;
  case 
MYSQL:
   
$this->results[$key]=@mysql_query($sql$this->conn);
   
$this->count[$key]=@mysql_num_rows($this->results[$key], $this->conn);
   break;
  }
  
$this->index[$key]=0;
 }
 
/*
  Execute- runs a query on the database connection but does not save results,
           used for queries of type INSERT, UPDATE, etc.
  @param: $sql- the SQL string
 */
 
function execute($sql) {
  
$sql=$this->parse($sql);
  
$this->debug($sql);
  switch(
$this->type) {
  case 
ACCESS:
   
$this->conn->Execute($sql);
   break;
  case 
MYSQL:
   @
mysql_query($sql$this->conn);
   break;
  }
 }
 
/*
  Next- moves to the next record in the results
  @param: $key- an identifier to a stored results resource
 */
 
function next($key=0) {
  switch(
$this->type) {
  case 
ACCESS:
   if(!
$this->results[$key]->EOF())
    
$this->results[$key]->MoveNext();
      break;
  case 
MYSQL:
   if(
$this->index[$key]<$this->count[$key])
    
$this->index[$key]++;
   break;
  }
   }
 
/*
  Move- moves a specified number of records in the results, or last record
  @param: $count- the nmber of records to move at maximum
  @param: $key- an identifier to a stored results resource
 */
 
function move($count$key=0) {
  switch(
$this->type) {
  case 
ACCESS:
   
$this->results[$key]->Move($count);
      break;
  case 
MYSQL:
   
$this->index[$key]=min(array($this->count[$key]-1$this->index[$key]+$count));
   break;
  }
 }
 
/*
  Result- returns the value from the specified field of the current record
  @param: $field- the field name of the desired result
  @param: $key- an identifier to a stored results resource
 */
 
function result($field$key=0) {
  switch(
$this->type) {
  case 
ACCESS:
   return 
$this->results[$key]->Fields[$field]->Value;
   break;
  case 
MYSQL:
   return @
mysql_result($this->results[$key], $this->index[$key], $field);
   break;
  }
 }
 
/*
  EOF- returns true if end of file is reached for the current result resource
  @param: $key- an identifier to a stored results resource
 */
 
function eof($key=0) {
  switch(
$this->type) {
  case 
ACCESS:
   return 
$this->results[$key]->EOF();
   break;
  case 
MYSQL:
   return (
$this->index[$key]>=$this->count[$key]);
   break;
  }
 }
 
/*
  Clear- clears the result resources
  @param: $key- an identifier to the results resource to be cleared
 */
 
function clear($key=0) {
  unset(
$this->results[$key], $this->index[$key], $this->count[$key]);
 }
 
/*
  Close- closes the database connection
 */
 
function close() {
  switch(
$this->type){
  case 
ACCESS:
   
$this->conn->Close();
   break;
  case 
MYSQL:
   @
mysql_close($this->conn);
   break;
  }
  unset(
$this->results$this->conn);
 }
 
/*
  (private) Parse- modifies the SQL statement to translate from the standard query
                   syntax to the specific database syntax
  @param: $sql- the SQL string to parse
  @return: the parsed SQL string
 */
 
function parse($sql) {
  switch(
$this->type) {
  case 
ACCESS:
   
$sql=str_replace("\'""''"$sql);
      break;
  case 
MYSQL:
   
$sql=str_replace("[""`"str_replace("]""`"$sql));
   break;
  }
  return 
$sql;
 }
 
/*
  (private) Debug- prints a debug message to the screen when enabled
  @param: $debug- the debug message to print
 */
 
function debug($debug) {
  if(
DEBUG) {
   echo 
$debug;
   
flush();
   
ob_flush();
  }
 }
// end database class
?>
  #3 (permalink)  
Antiguo 28/05/2007, 06:58
 
Fecha de Ingreso: marzo-2003
Mensajes: 164
Antigüedad: 21 años, 1 mes
Puntos: 0
Re: Conexion Php general a cualquier bd

gracias por tu ayuda lo revisare
  #4 (permalink)  
Antiguo 28/05/2007, 08:01
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Re: Conexion Php general a cualquier bd

Te recomiendo usar algo mas estandar, y que esta habilitado por defecto en las nuevas versiones de PHP, aparte de que esta programado en C y es mucho mas rapido.

Utiliza PDO, hay varios tutoriales en la web sobre como utilizarlo con PHP.

Saludos.
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:50.