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{
private static $_instance = NULL;
private $conId;
private $host;
private $user;
private $password;
private $database;
/***************************************************/
/* <SINGLETON> */
/***************************************************/
static
public function getInstance
(array $param_db) {
if (self::$_instance == NULL)
{
self::$_instance = new self($param_db);
}
return self::$_instance;
}//fin getInstance
/***************************************************/
/* </SINGLETON> */
/***************************************************/
public function __construct
(array $options=array()) {
{
throw new Exception('Invalid number of connection parameters');
}
foreach($options as $parameter=>$value)
{
if(!$value)
{
throw new Exception('Parametros Invalidos'.$parameter);
}
$this->{$parameter} = $value;
}
$this->connectDB();
}
// conectar la base de datos
private function connectDB()
{
if(!$this->conId=mysql_connect($this->host,$this->user,$this->password)) {
throw new Exception('Error al conectar al Server');
}
{
throw new Exception('Error al seleccionar la Base de datos');
}
}
// ejecutar consulta
public function query($query)
{
{
throw new Exception('Error performing query'.$query);
}
return new Result($this,$this->result);
}
}
****Factory.class*****
class Db_Factory
{
// The factory method
public static function &factory($type, $param_db)
{
{
//$classname = MySQL::getInstance($param_db);
return $classname;
} else {
throw new Exception ('Driver not found');
}
}
}
****Result.class****
class Result
{
private $mysql;
private $result;
public function __construct
(MySQL $mysql, $result) {
$this->result=$result;
}
// fetch row
public function fetchAll()
{
//return mysql_fetch_assoc($this->result);
}
return $rows;
}
// numero de registros devueltos
public function countRows()
{
throw new Exception('Error counting rows');
}
return $rows;
}
// count affected rows
public function countAffectedRows()
{
throw new Exception('Error counting affected rows');
}
return $rows;
}
// get insert ID
public function getInsertID()
{
throw new Exception('Error getting ID');
}
return $id;
}
// seek rows
public function seekRow($row=0)
{
if(!int($row)||$row<0){
throw new Exception('Invalid result set offset');
}
throw new Exception('Error seeking data');
}
}
// return result set
public function getResult()
{
return $this->result;
}
}
¿Como usar ?
Código php:
Ver original$param_db = array('host'=>$db_host, 'user'=>$db_user,
'password'=>$db_password,
'database'=>$db_database);
// conectar a MySQL
$db = Db_Factory::factory('MySQL',$param_db);
try
{
$consulta= "INSERT INTO tabla ( email, nombre)VALUES('[email protected]', 'pepito')"; $resultado = $db->query($consulta);
/***aca el problema***/
$ultimoId = $db->getInsertID() ; //extraigo el ultimo id
}
catch(Exception $e)
{
echo $e->getMessage();
}
Les agradeceria que pudieran encontrar alguna solucion
Muchas gracias por la ayuda..