Ver Mensaje Individual
  #5 (permalink)  
Antiguo 13/04/2009, 12:24
Avatar de Triby
Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: problema en la insercion de la base de datos y un warning

Ejemplo de una clase sencilla para mysql, tomada de PunBB:

Código php:
Ver original
  1. <?php
  2. /***********************************************************************
  3.  
  4.   Copyright (C) 2002-2005  Rickard Andersson ([email protected])
  5.  
  6.   This file is part of PunBB.
  7.  
  8.   PunBB is free software; you can redistribute it and/or modify it
  9.   under the terms of the GNU General Public License as published
  10.   by the Free Software Foundation; either version 2 of the License,
  11.   or (at your option) any later version.
  12.  
  13.   PunBB is distributed in the hope that it will be useful, but
  14.   WITHOUT ANY WARRANTY; without even the implied warranty of
  15.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.   GNU General Public License for more details.
  17.  
  18.   You should have received a copy of the GNU General Public License
  19.   along with this program; if not, write to the Free Software
  20.   Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21.   MA  02111-1307  USA
  22.  
  23. ************************************************************************/
  24. if(!defined('PUN')) {
  25.     header('HTTP/1.0 403 Forbidden');
  26.     die('You don\'t have permission to browse this directory or open the file.');
  27. }
  28.  
  29. // Make sure we have built in support for MySQL
  30. if (!function_exists('mysql_connect'))
  31.     exit('This PHP environment doesn\'t have MySQL support built in. MySQL support is required if you want to use a MySQL database to run this forum. Consult the PHP documentation for further assistance.');
  32.  
  33. class DBLayer {
  34.     var $prefix;
  35.     var $link_id;
  36.     var $query_result;
  37.     var $saved_queries = array();
  38.     var $show_queries = false;
  39.     var $num_queries = 0;
  40.  
  41.     function DBLayer($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect, $showq = false) {
  42.         $this->prefix = $db_prefix;
  43.         $this->show_queries = $showq;
  44.         if ($p_connect)
  45.             $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password);
  46.         else
  47.             $this->link_id = @mysql_connect($db_host, $db_username, $db_password);
  48.  
  49.         if ($this->link_id) {
  50.             if (@mysql_select_db($db_name, $this->link_id))
  51.                 return $this->link_id;
  52.             error('Unable to select database. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
  53.         }
  54.         error('Unable to connect to MySQL server. MySQL reported: '.mysql_error(), __FILE__, __LINE__);
  55.     }
  56.  
  57.     function start_transaction() {
  58.         return;
  59.     }
  60.  
  61.     function end_transaction()  {
  62.         return;
  63.     }
  64.  
  65.     function query($sql, $check_empty = false, $unbuffered = false) {
  66.         if ($this->show_queries)
  67.             $q_start = microtime();
  68.  
  69.         if ($unbuffered)
  70.             $this->query_result = @mysql_unbuffered_query($sql, $this->link_id);
  71.         else
  72.             $this->query_result = @mysql_query($sql, $this->link_id);
  73.         if ($this->query_result) {
  74.             if ($this->show_queries)
  75.                 $this->saved_queries[] = array($sql, sprintf('%.5f', microtime() - $q_start));
  76.             ++$this->num_queries;
  77.             // If check_empty = true and there were no rows loaded, return false
  78.             if($check_empty && $this->num_rows($this->query_result) < 1)
  79.                 return false;
  80.             return $this->query_result;
  81.         }
  82.         if ($this->show_queries)
  83.             $this->saved_queries[] = array($sql, 0);
  84.         return false;
  85.     }
  86.  
  87.     function result($query_id = 0, $row = 0) {
  88.         return ($query_id) ? @mysql_result($query_id, $row) : false;
  89.     }
  90.  
  91.     function fetch_assoc($query_id = 0) {
  92.         return ($query_id) ? @mysql_fetch_assoc($query_id) : false;
  93.     }
  94.  
  95.     function fetch_row($query_id = 0) {
  96.         return ($query_id) ? @mysql_fetch_row($query_id) : false;
  97.     }
  98.  
  99.     function num_rows($query_id = 0) {
  100.         return ($query_id) ? @mysql_num_rows($query_id) : false;
  101.     }
  102.  
  103.     function affected_rows() {
  104.         return ($this->link_id) ? @mysql_affected_rows($this->link_id) : false;
  105.     }
  106.  
  107.     function insert_id() {
  108.         return ($this->link_id) ? @mysql_insert_id($this->link_id) : false;
  109.     }
  110.  
  111.     function get_num_queries() {
  112.         return $this->num_queries;
  113.     }
  114.  
  115.     function get_saved_queries() {
  116.         return $this->saved_queries;
  117.     }
  118.  
  119.     function free_result($query_id = false) {
  120.         return ($query_id) ? @mysql_free_result($query_id) : false;
  121.     }
  122.  
  123.     function escape($str) {
  124.         if (is_array($str))
  125.             return '';
  126.         if (function_exists('mysql_real_escape_string'))
  127.             $str = mysql_real_escape_string($str, $this->link_id);
  128.         else
  129.             $str = mysql_escape_string($str);
  130.         return $str;
  131.     }
  132.  
  133.     function error() {
  134.         $result['error_sql'] = @current(@end($this->saved_queries));
  135.         $result['error_no'] = @mysql_errno($this->link_id);
  136.         $result['error_msg'] = @mysql_error($this->link_id);
  137.         return $result;
  138.     }
  139.  
  140.     function close() {
  141.         if ($this->link_id)     {
  142.             if ($this->query_result)
  143.                 @mysql_free_result($this->query_result);
  144.             return @mysql_close($this->link_id);
  145.         }
  146.         return false;
  147.     }
  148. }
__________________
- León, Guanajuato
- GV-Foto