servidor = $host; $this->clave = trim($pass); if (trim($user) != "") { $this->usuario = $user; } else { die("Debes especificar un usuario para la base de datos"); } if (trim($bd) != "") { $this->basedatos = $bd; } else { die("Debes especificar una base de datos"); } $this->conectar(); } /** * @name conectar * Metodo que conecta a la bd devuelve false en caso de no * haberse podido conectar */ function conectar() { //Conexion con el servidor $this->conexion_id = @ mysql_connect($this->servidor, $this->usuario, $this->clave); if (!$this->conexion_id) { die("ERROR: No se pudo conectar a la BD"); return 0; } //seleccionar la base de datos if (!@ mysql_select_db($this->basedatos, $this->conexion_id)) { die("ERROR: Imposible abrir la bd ".$this->basedatos); return 0; } } /** * @name setQuery * @param strin sql Sentencia SQL * Este método recibe una cadena sql y la almacena en el atributo $_sql para su futuro uso */ function setQuery($sql) { $sql = trim($sql); $this->_sql = $sql; } /** * @name query * @param string sql Sentencia sql a realizar * @return consulta_id */ function query() { $this->consulta_id = mysql_query($this->_sql, $this->conexion_id); if (!$this->consulta_id) { $this->errno = mysql_errno(); $this->error = mysql_error(); return false; } //Si hemos tenido exito en la consulta devuelve el identificador de la consulta si no devuelve 0 return $this->consulta_id; } /** * @name beginTransaction * Comienza la transaccion */ function beginTransaction() { $this->consulta_id = mysql_query("SET AUTOCOMMIT=0"); $this->consulta_id = mysql_query("BEGIN"); if (!$this->consulta_id) return false; //unset ($this->consulta_id); return true; } /** * @name commit * Compromete una transaccion en caso de haber sido completada correctamente * @return true o false */ function commit() { $this->consulta_id = mysql_query("COMMIT", $this->conexion_id); //$this->cerrarConexion(); //unset ($this->consulta_id); return true; } function rollback(){ $this->consulta_id = mysql_query("ROLLBACK", $this->conexion_id); //$this->cerrarConexion(); //unset ($this->consulta_id); return true; } /** * @name cerrarConexion * Cierra la conexion con mysql */ function cerrarConexion() { if ($this->conexion_id) { mysql_close($this->conexion_id); } } /** * @name numCampos * Devuelve el numero de campos en una consulta */ function getNumCampos() { if ($cur = $this->query()) { return mysql_num_fields($cur); } else { return false; } } /** * @name numRegistros * Devuelve el numero de registros obtenidos en la consulta */ function getNumRegistros() { if ($cur = $this->query()) { return intval(mysql_num_rows($cur)); } else { return false; } } /** * @name loadObjectList * Regresa un arreglo con una lista de objetos obtenidos en la consulta */ function loadObjectList($key = '') { if (!($cur = $this->query())) { return null; } $array = array (); while ($row = mysql_fetch_object($cur)) { if ($key) { $array[$row-> $key] = $row; } else { $array[] = $row; } } mysql_free_result($cur); return $array; } /** * @name loadObject * @param object * Almacena los resultados de la consulta en el objeto especificado */ function loadObject( &$object ) { if ($cur = $this->query()) { if ($object = mysql_fetch_object( $cur )) { mysql_free_result( $cur ); return true; } else { $object = null; return false; } } else { return false; } } /** * Document::db_insertObject() * * { Description } * * @param [type] $keyName * @param [type] $verbose */ function insertObject( $table, &$object, $keyName = NULL, $verbose=false ) { $fmtsql = "INSERT INTO $table ( %s ) VALUES ( %s ) "; foreach (get_object_vars( $object ) as $k => $v) { if (is_array($v) or is_object($v) or $v === NULL) { continue; } if ($k[0] == '_') { // internal field continue; } $fields[] = "`$k`"; $values[] = "'" . $this->getEscaped( $v ) . "'"; } $this->setQuery( sprintf( $fmtsql, implode( ",", $fields ) , implode( ",", $values ) ) ); ($verbose) && print "$sql
\n"; if (!$this->query()) { return false; } $id = mysql_insert_id(); ($verbose) && print "id=[$id]
\n"; if ($keyName && $id) { $object->$keyName = $id; } return true; } /** * Document::db_updateObject() * * { Description } * * @param [type] $updateNulls */ function updateObject( $table, &$object, $keyName, $updateNulls=true ) { $fmtsql = "UPDATE $table SET %s WHERE %s"; foreach (get_object_vars( $object ) as $k => $v) { if( is_array($v) or is_object($v) or $k[0] == '_' ) { // internal or NA field continue; } if( $k == $keyName ) { // PK not to be updated $where = "$keyName='" . $this->getEscaped( $v ) . "'"; continue; } if ($v === NULL && !$updateNulls) { continue; } if( $v == '' ) { $val = "''"; } else { $val = "'" . $this->getEscaped( $v ) . "'"; } $tmp[] = "`$k`=$val"; } //print_r($tmp); $this->setQuery( sprintf( $fmtsql, implode( ",", $tmp ) , $where ) ); return $this->query(); } function updateObjectRelation( $table, $campos, &$object, $keyName, $updateNulls=true ) { $fmtsql = "UPDATE $table SET %s WHERE %s"; foreach (get_object_vars( $object ) as $k => $v) { if( is_array($v) or is_object($v) or $k[0] == '_' ) { // internal or NA field continue; } if( $k == $keyName ) { // PK not to be updated $where = "$keyName='" . $this->getEscaped( $v ) . "'"; continue; } if ($v === NULL && !$updateNulls) { continue; } if(in_array($k, $campos)){ if( $v == '' ) { $val = "''"; } else { $val = "'" . $this->getEscaped( $v ) . "'"; } $tmp[] = "`$k`=$val"; }else{ continue; } } //print_r($tmp); //echo (sprintf( $fmtsql, implode( ",", $tmp ) , $where)); $this->setQuery( sprintf( $fmtsql, implode( ",", $tmp ) , $where ) ); return $this->query(); } //$table, $campos, &$object, $keyName, $updateNulls=true function insertObjectRelation( $table, $campos, &$object, $keyName = NULL) { $fmtsql = "INSERT INTO $table ( %s ) VALUES ( %s ) "; foreach (get_object_vars( $object ) as $k => $v) { if (is_array($v) or is_object($v) or $v === NULL) { continue; } if ($k[0] == '_') { // internal field continue; } if(in_array($k, $campos)){ $fields[] = "`$k`"; $values[] = "'" . $this->getEscaped( $v ) . "'"; } } //echo sprintf( $fmtsql, implode( ",", $fields ) , implode( ",", $values ) ); $this->setQuery( sprintf( $fmtsql, implode( ",", $fields ) , implode( ",", $values ) ) ); if (!$this->query()) { return false; } $id = mysql_insert_id(); if ($keyName && $id) { $object->$keyName = $id; } return true; } /** * Get a database escaped string * @return string */ function getEscaped( $text ) { return mysql_escape_string( $text ); } /** * @name loadArray * Regresa un arreglo asociativo con los registros obtenidos en la consulta */ function loadArray() { if (!($cur = $this->query())) { return null; } $array = array (); $i = 0; while ($row = mysql_fetch_assoc($cur)) { $array[] = $row; } mysql_free_result($cur); return $array; } /** * @name loadRow * Regresa un arreglo con el primer registr de la consulta */ function loadRow() { if (!($cur = $this->query())) { return null; } $ret = null; if ($row = mysql_fetch_assoc($cur)) { $ret = $row; } mysql_free_result($cur); return $ret; } /** * @name loadRowList * Regresa un arreglo asociativo y con indices */ function loadRowList() { if (!($cur = $this->query())) { return null; } $array = array (); while ($row = mysql_fetch_array($cur)) { $array[] = $row; } mysql_free_result($cur); return $array; } /** * @name loadRowValues * Regresa un arreglo con todos los valores de la consulta */ function loadRowValues() { if (!($cur = $this->query())) { return null; } $array = array (); while ($row = mysql_fetch_array($cur)) { $array[] = $row[0]; } mysql_free_result($cur); return $array; } /** * This method loads the first field of the first row returned by the query. * * @return The value returned in the query or null if the query failed. */ function loadResult() { if (!($cur = $this->query())) { return null; } $ret = null; if ($row = mysql_fetch_row( $cur )) { $ret = $row[0]; } mysql_free_result( $cur ); return $ret; } /** * @name getError */ function getError(){ return $this->error; } /** * @name getErrorNum */ function getErrorNum(){ return $this->erno; } /** * Regresa el ultimo ID agregado */ function insertid() { return mysql_insert_id(); } } //Fin de la clase database ?>
Parse error: syntax error, unexpected end of file in /home/alumayab/public_html/aluminioslaviga.com.mx/lib/widgets.html.php on line 156