Ver Mensaje Individual
  #1 (permalink)  
Antiguo 19/09/2008, 08:25
Avatar de mauricioikem
mauricioikem
 
Fecha de Ingreso: septiembre-2008
Mensajes: 17
Antigüedad: 15 años, 7 meses
Puntos: 0
Problema classes mysql_insert_id

Hola gente tengo un pequeño problema al querer utilizar el metodo getInsertID() definida en una de las clases (Result.class) para extraer el id del ultimo registro insertado en la base. Antes que nada utilizo tres archivos.
Paso a mostrar las clases.

***DB_Mysql.class***
Código php:
Ver original
  1. class MySQL
  2. {
  3.     private static $_instance = NULL;
  4.     private $conId;
  5.     private $host;
  6.     private $user;
  7.     private $password;
  8.     private $database;
  9.  
  10.     /***************************************************/
  11.     /* <SINGLETON>                                     */
  12.     /***************************************************/    
  13.     static public function getInstance(array $param_db)
  14.     {
  15.         if (self::$_instance == NULL)
  16.         {
  17.             self::$_instance = new self($param_db);
  18.         }
  19.         return self::$_instance;
  20.     }//fin getInstance
  21.     /***************************************************/
  22.     /* </SINGLETON>                                      */
  23.     /***************************************************/
  24.        
  25.     public function __construct(array $options=array())
  26.     {
  27.         if(count($options)<4)
  28.         {
  29.             throw new Exception('Invalid number of connection parameters');
  30.         }
  31.        
  32.         foreach($options as $parameter=>$value)
  33.         {
  34.             if(!$value)
  35.             {
  36.                 throw new Exception('Parametros Invalidos'.$parameter);
  37.             }
  38.             $this->{$parameter} = $value;
  39.         }
  40.         $this->connectDB();
  41.     }
  42.    
  43.     // conectar la base de datos
  44.     private function connectDB()
  45.     {
  46.         if(!$this->conId=mysql_connect($this->host,$this->user,$this->password))
  47.         {
  48.             throw new Exception('Error al conectar al Server');
  49.         }
  50.         if(!mysql_select_db($this->database,$this->conId))
  51.         {
  52.             throw new Exception('Error al seleccionar la Base de datos');
  53.         }
  54.     }
  55.    
  56.     // ejecutar consulta
  57.     public function query($query)
  58.     {
  59.         if(!$this->result=mysql_query($query,$this->conId))
  60.         {
  61.             throw new Exception('Error performing query'.$query);
  62.         }
  63.        
  64.         return new Result($this,$this->result);
  65.     }
  66. }  
  67.  
  68. ****Factory.class*****
  69.  
  70. class Db_Factory
  71. {    
  72.     // The factory method
  73.     public static function &factory($type, $param_db)
  74.     {
  75.         if (class_exists($type, false))
  76.         {
  77.             $classname = call_user_func( array($type, 'getInstance'),$param_db );
  78.             //$classname = MySQL::getInstance($param_db);
  79.             return $classname;
  80.         } else {
  81.            throw new Exception ('Driver not found');
  82.         }
  83.     }
  84. }  
  85.  
  86.  
  87. ****Result.class****
  88.  
  89. class Result
  90. {
  91.     private $mysql;
  92.     private $result;
  93.    
  94.     public function __construct(MySQL $mysql, $result)
  95.     {
  96.         $this->mysql=$mysql;
  97.         $this->result=$result;
  98.     }
  99.    
  100.     // fetch row
  101.     public function fetchAll()
  102.     {
  103.         //return mysql_fetch_assoc($this->result);
  104.           $rows = array();      
  105.           while ($row = mysql_fetch_array( $this->result, MYSQL_ASSOC )) {
  106.               array_push($rows, $row);
  107.           }
  108.          
  109.           return $rows;
  110.     }
  111.    
  112.     // numero de registros devueltos
  113.     public function countRows()
  114.     {
  115.         if(!$rows=mysql_num_rows($this->result)){
  116.             throw new Exception('Error counting rows');
  117.         }
  118.         return $rows;
  119.     }
  120.    
  121.     // count affected rows
  122.     public function countAffectedRows()
  123.     {
  124.         if(!$rows=mysql_affected_rows($this->mysql->conId)){
  125.             throw new Exception('Error counting affected rows');
  126.         }
  127.         return $rows;
  128.     }
  129.    
  130.     // get insert ID
  131.     public function getInsertID()
  132.     {
  133.         if(!$id=mysql_insert_id($this->mysql->conId)){
  134.             throw new Exception('Error getting ID');
  135.         }
  136.         return $id;
  137.     }
  138.    
  139.     // seek rows
  140.     public function seekRow($row=0)
  141.     {
  142.         if(!int($row)||$row<0){
  143.             throw new Exception('Invalid result set offset');
  144.         }
  145.         if(!mysql_data_seek($this->result,$row)){
  146.             throw new Exception('Error seeking data');
  147.         }
  148.     }
  149.    
  150.     // return result set
  151.     public function getResult()
  152.     {
  153.         return $this->result;
  154.     }
  155. }

¿Como usar ?
Código php:
Ver original
  1. $param_db = array('host'=>$db_host,
  2.                    'user'=>$db_user,
  3.                    'password'=>$db_password,
  4.                    'database'=>$db_database);
  5. // conectar a MySQL      
  6. $db = Db_Factory::factory('MySQL',$param_db);
  7. try
  8. {
  9. $consulta= "INSERT INTO tabla ( email, nombre)VALUES('[email protected]', 'pepito')";
  10. $resultado = $db->query($consulta);
  11.  
  12. /***aca el problema***/
  13. $ultimoId = $db->getInsertID()  ; //extraigo el ultimo id
  14.            
  15.     }
  16.    catch(Exception $e)
  17.             {
  18.     echo $e->getMessage();
  19.     exit();
  20. }
Les agradeceria que pudieran encontrar alguna solucion
Muchas gracias por la ayuda..

Última edición por GatorV; 19/09/2008 a las 08:38