Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Davide88

Erro ao listar resultados

Recommended Posts

BOAS! sou novo nestas andanças tenho algumas noçoes mas nao muitas e comprei um site que elabora vendas mas quando listo os produtos da me o seguinte erro Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/pagewebs/public_html/es/includes/classes/DB_ResultSet.class.php. on line 29, gostava de saber se alguem me pode ajudar pois o site assim nao fica nada bem .

 

 

Em baixo envio o código:

 

 

<?php
class DB_ResultSet
{
	/* PHP5
	public $Set;
	*/
	var $Set;
	/**
	 * @return DB_ResultSet()
	 * @desc Constructor
	 */
	function DB_ResultSet()
	{
	}
	/**
	 * @return array
	 * @desc Returns the result as an indexed array
	 */
	function FetchRow()
	{
		return mysql_fetch_row($this->Set);
	}
	/**
	 * @return array
	 * @desc Returns the result as an associative array
	 */
	function FetchAssoc()
	{
		
		return mysql_fetch_assoc($this->Set); 
	}
	/**
	 * @return void
	 * @param $format string
	 * @desc Echoes result as HTML table.
	 */
	function ResultAll($format="")
	{
		echo "<table $format>".$this->GetFieldsAll("th").$this->GetRowsAll()."</table>";
	}
	/**
	 * @return string
	 * @param $format string
	 * @desc Gets result as HTML table with the specified format.
	 */
	function GetResultAll($format="")
	{
		return "<table $format>".$this->GetFieldsAll("th").$this->GetRowsAll()."</table>";
	}
	/**
	 * @return string
	 * @param $tag string
	 * @param $format string
	 * @param $separator string
	 * @desc Gets fields names as HTML table headers ($tag = "th") or table data cells ($tag = "td") with the specified format for each one.
	 */
	function GetFieldsAll($tag="",$format="",$separator=";")
	{
		$tag = strtolower($tag);
		$tags = array("th","td");
		$echo = "";
		$prevtag = "";
		$posttag = "";

		if(!in_array($tag,$tags))
			$posttag = $separator;
		else
		{
			$prevtag = "<".$tag." ".$format.">";
			$posttag = "</".$tag.">";
		}
		$echo .= "<tr>";
		for($i=0; $i<$this->NumFields(); $i++)
			$echo .= $prevtag.$this->FieldName($i).$posttag;
		$echo .= "</tr>";
		return $echo;
	}
	/**
	 * @return string
	 * @param $distributecolumnsuniformly bool
	 * @param $num_format string
	 * @param $tr_format string
	 * @param $td_format string
	 * @desc Gets rows data as table rows with the specified format for each one and the specified format for each table data cell, and $num_format from numeric data.
	 */
	function GetRowsAll($distributecolumnsuniformly=false,$num_format="%s",$tr_format="",$td_format="")
	{
		$td_width = "";
		if($distributecolumnsuniformly)
			$td_width = (100 / $this->NumFields())."%' ";
		$echo = "";
		if($this->MoveFirst())
			while($reg = $this->FetchRow())
			{
				$echo .= "<tr ".$tr_format.">";
				for($i=0, $regsize=sizeof($reg); $i<$regsize; $i++)
				{
					$myreg = $reg[$i];
					$echo .= "<td ".$td_format;
					if($distributecolumnsuniformly && !eregi("width",$td_format))
						$echo .= " width='".$td_width;
					if(!eregi("align",$td_format))
						switch($this->FieldType($i))
						{
							case "numeric":
								$echo .= " align='right'";
								break;
							case "datetime":
								$echo .= " align='center'";
								break;
							case "string":
								break;
							case "unknown":
								$myreg = strip_tags($myreg);
								$echo .= " align='left'";
								break;
						}
					if($this->FieldType($i) == "numeric")
						$echo .= ">".sprintf($num_format, $myreg)."</td>";
					else
						$echo .= ">".htmlspecialchars($myreg)."</td>";
				}
				$echo .= "</tr>";
			}
		return $echo;
	}
	/**
	 * @return bool
	 * @param $rowindex int
	 * @desc Sets the inner row pointer to the given $rowindex. Return true if exists, false if not. Note: rows count starts in zero!
	 */
	function DataSeek($rowindex)
	{
		if($this->NumRows() > 0)
			return mysql_data_seek($this->Set,$rowindex);
		else
			return false;
	}
	/**
	 * @return bool
	 * @desc Sets the inner row pointer to the first row. Return true if exists, false if not.
	 */
	function MoveFirst()
	{
		return $this->DataSeek(0);
	}
	/**
	 * @return int
	 * @desc Returns number of rows.
	 */
	function NumRows()
	{
		return mysql_num_rows($this->Set);
	}
	/**
	 * @return int
	 * @desc Returns number of fields.
	 */
	function NumFields()
	{
		return mysql_num_fields($this->Set);
	}
	/**
	 * @return string
	 * @param $fieldindex int
	 * @desc Gets the name of the specified field. Field indexes start in zero.
	 */
	function FieldName($fieldindex)
	{
		return mysql_field_name($this->Set,$fieldindex);
	}
	/**
	 * @return string
	 * @param $fieldindex int
	 * @desc Gets the type of the specified field. Field indexes start in zero. Types: numeric, string, datetime and unknown.
	 */
	function FieldType($fieldindex)
	{
		switch(mysql_field_type($this->Set,$fieldindex))
		{
			//types from MySQL-PHP
			case "int":
			case "real":
				return "numeric";
			case "string":
			case "blob":
				return "string";
			case "timestamp":
			case "year":
			case "date":
			case "time":
				return "datetime";
			case "null":
			default:
				return "unknown";
		}
	}
	/**
	 * @return bool
	 * @desc Free result memory (cleans Set).
	 */
	function FreeResult()
	{
		
		return mysql_free_result($this->Set);
	}
}
?>

 

AGRADECIA MUITO A AJUDA.

 

 

CUMPS

Compartilhar este post


Link para o post
Compartilhar em outros sites

faz o seguinte:

 

function FetchAssoc()
        {
                if( !$this->Set ) echo mysql_error();
                return mysql_fetch_assoc($this->Set); 
        }
vai te mostrar o motivo de ter falhado.

 

arrume.

 

ps: não está nessa classe a fonte do problema.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Boas, a mensagem dix o seguinte:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) AND timedesde < 1288016370 AND a.id_categoria = '48' ORDER BY mostrado ASC L' at line 1

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /home/pagewebs/public_html/es/includes/classes/DB_ResultSet.class.php on line 30

EM baixo envio lhe o ficheiro class de connection. Mais uma vez desculpe o incomodo mas se me poder ajudar era fine.

 

 

CUMPS

 

<?php
class DB_Connection
{
	/* PHP5
	public $ip;
	public $dbport;
	public $dbname;
	public $dbuser;
	public $dbpass;
	private $dbcon;
	*/
	var $ip;
	var $dbport;
	var $dbname;
	var $dbuser;
	var $dbpass;
	var $dbcon;
	/**
	 * @return DB_Connection()
	 * @desc Constructor
	 */
	function DB_Connection($ip="localhost", $dbport=3306, $dbname="", $dbuser="root", $dbpass="root", $connect=true)
	{
		$this->ip = $ip;
		$this->dbport = $dbport;
		$this->dbname = $dbname;
		$this->dbuser = $dbuser;
		$this->dbpass = $dbpass;
		if($connect)
		{
			$this->Connect();
		}
	}
	/**
	 * Obtiene el host (ip o nombre) junto con el puerto
	 *
	 * @return string
	 */
	function GetDBHost()
	{
		return $this->ip.":".$this->dbport;
	}
	/**
	 * @return bool
	 * @param bool $selectDB
	 * @param bool $new
	 * @desc Connects to server
	 */
	function Connect($selectDB=true, $new=true)
	{
		if($this->dbcon = mysql_connect($this->GetDBHost(), $this->dbuser, $this->dbpass, $new))
		{
			return $selectDB ? $this->SelectDB($this->dbname, $this->dbcon) : true;
		}
		return false;
	}
	/**
	 * @return bool
	 * @param $dbname string
	 * @desc Selects the database to work with. Returns true if exists, false if not.
	 */
	function SelectDB($dbname)
	{
		return @mysql_select_db($dbname, $this->dbcon);
	}
	/**
	 * @return mixed
	 * @param $SQLsentence string
	 * @desc Execute a SQL sentence
	 */
	function Execute($SQLsentence)
	{
		return @mysql_query($SQLsentence, $this->dbcon);
	}
	/**
	 * @return mixed
	 * @desc Get number of affected rows in previous SQL operation. Not yet operative for ODBC databases.
	 */
	function AffectedRows()
	{
		return @mysql_affected_rows($this->dbcon);
	}
	/**
	 * Returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query on success,
	 * 0 if the previous query does not generate an AUTO_INCREMENT value,
	 * or FALSE if no MySQL connection was established.
	 * @return mixed
	 */
	function Insert_id()
	{
		return mysql_insert_id($this->dbcon);
	}
	/**
	 * Comprueba si existe un registro en una tabla con el valor(es) especificado(s) para el campo(s) dado(s). El filtro sólo permite where.
	 *
	 * @param mixed $valores
	 * @param mixed $campos
	 * @param string $tabla
	 * @param string $filtro
	 * @return bool
	 */
	function Exists($valores, $campos, $tabla, $filtro="WHERE 1")
	{
		if(is_array($valores) && is_array($campos))
		{
			$size_valores = sizeof($valores);
			$size_campos = sizeof($campos);
			if($size_valores > 0 && $size_valores == $size_campos)
			{
				$query = "SELECT `{$campos[0]}` FROM `$tabla` $filtro";
				for($c=0; $c<$size_valores; $c++)
				{
					$query .= " AND `{$campos[$c]}` = '{$valores[$c]}'";
				}
			}
		}
		else
		{
			$query = "SELECT `$campos` FROM `$tabla` $filtro AND `$campos` = '$valores'";
		}
		return $this->ExistsQuery($query);
	}
	/**
	 * Comprueba si la consulta indicada devuelve al menos una fila
	 *
	 * @param string $query
	 * @return bool
	 */
	function ExistsQuery($query)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		return $rs->NumRows() > 0;
	}
	/**
	 * Obtiene la primera fila resultado de la consulta $query
	 *
	 * @param string $query
	 * @param bool $assoc
	 * @param bool $htmlentities
	 * @return array
	 */
	function GetRow($query, $assoc=true, $htmlentities=true)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		if($assoc)
		{
			if($reg = $rs->FetchAssoc())
			{
				return $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
		}
		else
		{
			if($reg = $rs->FetchRow())
			{
				return $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
		}
		return null;
	}
	/**
	 * Obtiene las filas resultado de la consulta $query como un array asociativo
	 *
	 * @param string $query
	 * @param bool $assoc
	 * @param bool $htmlentities
	 * @return array
	 */
	function GetRows($query, $assoc=true, $htmlentities=true)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		$rows = array();
		if($assoc)
		{
			while($reg = $rs->FetchAssoc())
			{
				$rows[] = $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
		}
		else
		{
			while($reg = $rs->FetchRow())
			{
				$rows[] = $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
		}
		return $rows;
	}
	/**
	 * Obtiene el número de filas resultantes de la consulta $query
	 *
	 * @param string $query
	 * @return int
	 */
	function GetCount($query)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		return $rs->NumRows();
	}
	/**
	 * Obtiene el valor de un campo del primer registro de una tabla filtrada (nulo si no hay registros)
	 *
	 * @param string $campo
	 * @param string $tabla
	 * @param string $filtro
	 * @param bool $addcomilla
	 * @param bool $htmlentities
	 * @return mixed
	 */
	function GetValue($campo, $tabla, $filtro="WHERE 1", $addcomilla=true, $htmlentities=true)
	{
		$query = $addcomilla ? "SELECT `$campo` FROM `$tabla` $filtro" : "SELECT $campo FROM $tabla $filtro";
		return $this->GetValueQuery($query, $htmlentities);
	}
	/**
	 * Enter description here...
	 *
	 * @param string $query
	 * @param bool $htmlentities
	 * @return mixed
	 */
	function GetValueQuery($query, $htmlentities=true)
	{
		
		$rs = new DB_ResultSet();
		if($rs->Set = $this->Execute($query))
		{
			if($reg = $rs->FetchRow())
			{
				return $htmlentities ? htmlentities($reg[0]) : $reg[0];
			}
		}
		return null;
	}
	/**
	 * Obtiene un array con los valores de un campo(s) de los registros de una tabla filtrada
	 *
	 * @param mixed $campos
	 * @param string $tabla
	 * @param string $filtro
	 * @param bool $htmlentities
	 * @return array
	 */
	function GetValues($campos, $tabla, $filtro="WHERE 1", $htmlentities=true)
	{
		$campos_select = "";
		if(is_array($campos))
		{
			for($c=0,$size=sizeof($campos); $c<$size; $c++)
			{
				if($c > 0)
				{
					$campos_select .= ",";
				}
				$campos_select .= "`{$campos[$c]}`";
			}
		}
		else
		{
			$campos_select = "`$campos`";
		}
		return $this->GetValuesQuery("SELECT $campos_select FROM `$tabla` $filtro", $htmlentities);
	}
	/**
	 * Obtiene un array con los valores resultado de una consulta
	 *
	 * @param string $query
	 * @param bool $htmlentities
	 * @return array
	 */
	function GetValuesQuery($query, $htmlentities=true)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		$values = array();
		while($reg = $rs->FetchAssoc())
		{
			if(sizeof($reg) > 1)
			{
				$values[] = $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
			else
			{
				$keys = array_keys($reg);
				$values[] = $htmlentities ? htmlentities($reg[$keys[0]]) : $reg[$keys[0]];
			}
		}
		return $values;
	}
	function GetArrayForHTMLSelect($index_field, $text_field, $table, $filter="", $option=null, $addcomilla=true)
	{
		$query = $addcomilla ? "SELECT `$index_field`, `$text_field` FROM `$table` $filter" : "SELECT $index_field, $text_field FROM $table $filter";
		return $this->GetArrayForHTMLSelectQuery($query, $option);
	}
	function GetArrayForHTMLSelectQuery($query, $option=null)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		$array = array();
		if(is_array($option) && is_array($option[0]))
		{
			foreach($option as $opt)
			{
				if(is_array($opt) && sizeof($opt) == 2)
				{
					$array[$opt[0]] = $opt[1];
				}
			}
		}
		elseif(is_array($option) && sizeof($option) == 2)
		{
			$array[$option[0]] = $option[1];
		}
		while($reg = $rs->FetchRow())
		{
			$array[$reg[0]] = $reg[1];
		}
		return $array;
	}
	/**
     * @return resource
     */
    function GetLinkIdentifier()
    {
    	return $this->dbcon;
    }
    /**
	 * @return bool
	 * @desc Closes the connection
	 */
	function Close()
	{
		return @mysql_close($this->dbcon);
	}
	/**
	 * @return string
	 * @desc Gets the last error message from an operation with this connection.
	 */
	function Error()
	{
		return @mysql_error($this->dbcon);
	}
	/**
     * @return void
     * @desc Destructor implementation to ensure that we close.
     */
    function _DB_Connection()
    {
        $this->Close();
    }
    /**
	 * @return string
	 * @param $tables array
	 * @param $exportstructure bool
	 * @param $exportdata bool
	 * @param $droptable bool
	 * @desc Gets the database as an SQL script. If $tables is empty, write down all tables.
	 */
	function GetSQLDatabase($tables=array(), $exportstructure=true, $exportdata=true, $droptable=true)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute("SELECT VERSION()");
		list($serverversion) = $rs->FetchRow();
		$sql = "# Internia .NET\r\n"
			."#\r\n"
			."# Host: ".$this->ip."   Database: ".$this->dbname."\r\n"
			."# --------------------------------------------------------\r\n"
			."# Server version $serverversion\r\n";
		if(sizeof($tables) > 0)
		{
			foreach($tables as $table)
			{
				$sql .= $this->GetSQLTable($table, $exportstructure, $exportdata, $droptable);
			}
		}
		else
		{
			$rs = new DB_ResultSet();
			$rs->Set = $this->Execute("SHOW TABLES");
			while(list($table) = $rs->FetchRow())
			{
				$sql .= $this->GetSQLTable($table, $exportstructure, $exportdata, $droptable);
			}
		}
		return $sql;
	}
	/**
	 * @return string
	 * @param $table string
	 * @param $exportstructure bool
	 * @param $exportdata bool
	 * @param $droptable bool
	 * @desc Gets a table as an SQL script.
	 */
	function GetSQLTable($table, $exportstructure=true, $exportdata=true, $droptable=true)
	{
		$sql = "";
		if($exportstructure)
		{
			$rs = new DB_ResultSet();
			$rs->Set = $this->Execute("SHOW CREATE TABLE `$table`");
			$reg = $rs->FetchAssoc();
			$sql .= "#\r\n"
				."# Table structure for table `$table`\r\n"
				."#\r\n"
				.($droptable ? "DROP TABLE IF EXISTS `$table`;\r\n" : "")
				.$reg["Create Table"].";\r\n";
		}
		if($exportdata)
		{
			$sql .= "#\r\n"
				."# Dumping data for table `$table`\r\n"
				."#\r\n";
			$rs->Set = $this->Execute("SELECT * FROM `$table`");
			while($reg = $rs->FetchRow())
			{
				$sql .= "INSERT INTO `$table` VALUES(";
				for($i=0, $regsize=sizeof($reg); $i < $regsize; $i++)
				{
					$myreg = $reg[$i];
					switch($rs->FieldType($i))
					{
						case "string":
						case "datetime":
						case "unknown":
							$myreg = "'".mysql_real_escape_string($myreg)."'";
					}
					$sql .= $myreg.(($i < $regsize-1) ? ", " : ");\r\n");
				}
			}
		}
		return $sql;
	}
	function GetAccessData()
	{
		return array(
			"ip"=>$this->ip,
			"dbport"=>$this->dbport,
			"dbname"=>$this->dbname,
			"dbuser"=>$this->dbuser,
			"dbpass"=>$this->dbpass,
			);
	}
	function SetAccessData($data, $connect=false, $selectDb=true, $new=true)
	{
		$this->ip = $data["ip"];
		$this->dbport = $data["dbport"];
		$this->dbname = $data["dbname"];
		$this->dbuser = $data["dbuser"];
		$this->dbpass = $data["dbpass"];

		if($connect)
		{
			return $this->Connect($selectDb, $new);
		}

		return true;
	}
	/**
	 * Aplica htmlentities sobre todos los elementos de un array
	 * private
	 *
	 * @param array $array
	 */
	function array_htmlentities($array)
	{
		$keys = array_keys($array);
		foreach($keys as $key)
		{
			$array[$key] = htmlentities($array[$key]);
		}
		return $array;
	}
}
?><?php
class DB_Connection
{
	/* PHP5
	public $ip;
	public $dbport;
	public $dbname;
	public $dbuser;
	public $dbpass;
	private $dbcon;
	*/
	var $ip;
	var $dbport;
	var $dbname;
	var $dbuser;
	var $dbpass;
	var $dbcon;
	/**
	 * @return DB_Connection()
	 * @desc Constructor
	 */
	function DB_Connection($ip="localhost", $dbport=3306, $dbname="", $dbuser="root", $dbpass="root", $connect=true)
	{
		$this->ip = $ip;
		$this->dbport = $dbport;
		$this->dbname = $dbname;
		$this->dbuser = $dbuser;
		$this->dbpass = $dbpass;
		if($connect)
		{
			$this->Connect();
		}
	}
	/**
	 * Obtiene el host (ip o nombre) junto con el puerto
	 *
	 * @return string
	 */
	function GetDBHost()
	{
		return $this->ip.":".$this->dbport;
	}
	/**
	 * @return bool
	 * @param bool $selectDB
	 * @param bool $new
	 * @desc Connects to server
	 */
	function Connect($selectDB=true, $new=true)
	{
		if($this->dbcon = mysql_connect($this->GetDBHost(), $this->dbuser, $this->dbpass, $new))
		{
			return $selectDB ? $this->SelectDB($this->dbname, $this->dbcon) : true;
		}
		return false;
	}
	/**
	 * @return bool
	 * @param $dbname string
	 * @desc Selects the database to work with. Returns true if exists, false if not.
	 */
	function SelectDB($dbname)
	{
		return @mysql_select_db($dbname, $this->dbcon);
	}
	/**
	 * @return mixed
	 * @param $SQLsentence string
	 * @desc Execute a SQL sentence
	 */
	function Execute($SQLsentence)
	{
		return @mysql_query($SQLsentence, $this->dbcon);
	}
	/**
	 * @return mixed
	 * @desc Get number of affected rows in previous SQL operation. Not yet operative for ODBC databases.
	 */
	function AffectedRows()
	{
		return @mysql_affected_rows($this->dbcon);
	}
	/**
	 * Returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query on success,
	 * 0 if the previous query does not generate an AUTO_INCREMENT value,
	 * or FALSE if no MySQL connection was established.
	 * @return mixed
	 */
	function Insert_id()
	{
		return mysql_insert_id($this->dbcon);
	}
	/**
	 * Comprueba si existe un registro en una tabla con el valor(es) especificado(s) para el campo(s) dado(s). El filtro sólo permite where.
	 *
	 * @param mixed $valores
	 * @param mixed $campos
	 * @param string $tabla
	 * @param string $filtro
	 * @return bool
	 */
	function Exists($valores, $campos, $tabla, $filtro="WHERE 1")
	{
		if(is_array($valores) && is_array($campos))
		{
			$size_valores = sizeof($valores);
			$size_campos = sizeof($campos);
			if($size_valores > 0 && $size_valores == $size_campos)
			{
				$query = "SELECT `{$campos[0]}` FROM `$tabla` $filtro";
				for($c=0; $c<$size_valores; $c++)
				{
					$query .= " AND `{$campos[$c]}` = '{$valores[$c]}'";
				}
			}
		}
		else
		{
			$query = "SELECT `$campos` FROM `$tabla` $filtro AND `$campos` = '$valores'";
		}
		return $this->ExistsQuery($query);
	}
	/**
	 * Comprueba si la consulta indicada devuelve al menos una fila
	 *
	 * @param string $query
	 * @return bool
	 */
	function ExistsQuery($query)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		return $rs->NumRows() > 0;
	}
	/**
	 * Obtiene la primera fila resultado de la consulta $query
	 *
	 * @param string $query
	 * @param bool $assoc
	 * @param bool $htmlentities
	 * @return array
	 */
	function GetRow($query, $assoc=true, $htmlentities=true)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		if($assoc)
		{
			if($reg = $rs->FetchAssoc())
			{
				return $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
		}
		else
		{
			if($reg = $rs->FetchRow())
			{
				return $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
		}
		return null;
	}
	/**
	 * Obtiene las filas resultado de la consulta $query como un array asociativo
	 *
	 * @param string $query
	 * @param bool $assoc
	 * @param bool $htmlentities
	 * @return array
	 */
	function GetRows($query, $assoc=true, $htmlentities=true)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		$rows = array();
		if($assoc)
		{
			while($reg = $rs->FetchAssoc())
			{
				$rows[] = $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
		}
		else
		{
			while($reg = $rs->FetchRow())
			{
				$rows[] = $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
		}
		return $rows;
	}
	/**
	 * Obtiene el número de filas resultantes de la consulta $query
	 *
	 * @param string $query
	 * @return int
	 */
	function GetCount($query)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		return $rs->NumRows();
	}
	/**
	 * Obtiene el valor de un campo del primer registro de una tabla filtrada (nulo si no hay registros)
	 *
	 * @param string $campo
	 * @param string $tabla
	 * @param string $filtro
	 * @param bool $addcomilla
	 * @param bool $htmlentities
	 * @return mixed
	 */
	function GetValue($campo, $tabla, $filtro="WHERE 1", $addcomilla=true, $htmlentities=true)
	{
		$query = $addcomilla ? "SELECT `$campo` FROM `$tabla` $filtro" : "SELECT $campo FROM $tabla $filtro";
		return $this->GetValueQuery($query, $htmlentities);
	}
	/**
	 * Enter description here...
	 *
	 * @param string $query
	 * @param bool $htmlentities
	 * @return mixed
	 */
	function GetValueQuery($query, $htmlentities=true)
	{
		
		$rs = new DB_ResultSet();
		if($rs->Set = $this->Execute($query))
		{
			if($reg = $rs->FetchRow())
			{
				return $htmlentities ? htmlentities($reg[0]) : $reg[0];
			}
		}
		return null;
	}
	/**
	 * Obtiene un array con los valores de un campo(s) de los registros de una tabla filtrada
	 *
	 * @param mixed $campos
	 * @param string $tabla
	 * @param string $filtro
	 * @param bool $htmlentities
	 * @return array
	 */
	function GetValues($campos, $tabla, $filtro="WHERE 1", $htmlentities=true)
	{
		$campos_select = "";
		if(is_array($campos))
		{
			for($c=0,$size=sizeof($campos); $c<$size; $c++)
			{
				if($c > 0)
				{
					$campos_select .= ",";
				}
				$campos_select .= "`{$campos[$c]}`";
			}
		}
		else
		{
			$campos_select = "`$campos`";
		}
		return $this->GetValuesQuery("SELECT $campos_select FROM `$tabla` $filtro", $htmlentities);
	}
	/**
	 * Obtiene un array con los valores resultado de una consulta
	 *
	 * @param string $query
	 * @param bool $htmlentities
	 * @return array
	 */
	function GetValuesQuery($query, $htmlentities=true)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		$values = array();
		while($reg = $rs->FetchAssoc())
		{
			if(sizeof($reg) > 1)
			{
				$values[] = $htmlentities ? $this->array_htmlentities($reg) : $reg;
			}
			else
			{
				$keys = array_keys($reg);
				$values[] = $htmlentities ? htmlentities($reg[$keys[0]]) : $reg[$keys[0]];
			}
		}
		return $values;
	}
	function GetArrayForHTMLSelect($index_field, $text_field, $table, $filter="", $option=null, $addcomilla=true)
	{
		$query = $addcomilla ? "SELECT `$index_field`, `$text_field` FROM `$table` $filter" : "SELECT $index_field, $text_field FROM $table $filter";
		return $this->GetArrayForHTMLSelectQuery($query, $option);
	}
	function GetArrayForHTMLSelectQuery($query, $option=null)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute($query);
		$array = array();
		if(is_array($option) && is_array($option[0]))
		{
			foreach($option as $opt)
			{
				if(is_array($opt) && sizeof($opt) == 2)
				{
					$array[$opt[0]] = $opt[1];
				}
			}
		}
		elseif(is_array($option) && sizeof($option) == 2)
		{
			$array[$option[0]] = $option[1];
		}
		while($reg = $rs->FetchRow())
		{
			$array[$reg[0]] = $reg[1];
		}
		return $array;
	}
	/**
     * @return resource
     */
    function GetLinkIdentifier()
    {
    	return $this->dbcon;
    }
    /**
	 * @return bool
	 * @desc Closes the connection
	 */
	function Close()
	{
		return @mysql_close($this->dbcon);
	}
	/**
	 * @return string
	 * @desc Gets the last error message from an operation with this connection.
	 */
	function Error()
	{
		return @mysql_error($this->dbcon);
	}
	/**
     * @return void
     * @desc Destructor implementation to ensure that we close.
     */
    function _DB_Connection()
    {
        $this->Close();
    }
    /**
	 * @return string
	 * @param $tables array
	 * @param $exportstructure bool
	 * @param $exportdata bool
	 * @param $droptable bool
	 * @desc Gets the database as an SQL script. If $tables is empty, write down all tables.
	 */
	function GetSQLDatabase($tables=array(), $exportstructure=true, $exportdata=true, $droptable=true)
	{
		$rs = new DB_ResultSet();
		$rs->Set = $this->Execute("SELECT VERSION()");
		list($serverversion) = $rs->FetchRow();
		$sql = "# Internia .NET\r\n"
			."#\r\n"
			."# Host: ".$this->ip."   Database: ".$this->dbname."\r\n"
			."# --------------------------------------------------------\r\n"
			."# Server version $serverversion\r\n";
		if(sizeof($tables) > 0)
		{
			foreach($tables as $table)
			{
				$sql .= $this->GetSQLTable($table, $exportstructure, $exportdata, $droptable);
			}
		}
		else
		{
			$rs = new DB_ResultSet();
			$rs->Set = $this->Execute("SHOW TABLES");
			while(list($table) = $rs->FetchRow())
			{
				$sql .= $this->GetSQLTable($table, $exportstructure, $exportdata, $droptable);
			}
		}
		return $sql;
	}
	/**
	 * @return string
	 * @param $table string
	 * @param $exportstructure bool
	 * @param $exportdata bool
	 * @param $droptable bool
	 * @desc Gets a table as an SQL script.
	 */
	function GetSQLTable($table, $exportstructure=true, $exportdata=true, $droptable=true)
	{
		$sql = "";
		if($exportstructure)
		{
			$rs = new DB_ResultSet();
			$rs->Set = $this->Execute("SHOW CREATE TABLE `$table`");
			$reg = $rs->FetchAssoc();
			$sql .= "#\r\n"
				."# Table structure for table `$table`\r\n"
				."#\r\n"
				.($droptable ? "DROP TABLE IF EXISTS `$table`;\r\n" : "")
				.$reg["Create Table"].";\r\n";
		}
		if($exportdata)
		{
			$sql .= "#\r\n"
				."# Dumping data for table `$table`\r\n"
				."#\r\n";
			$rs->Set = $this->Execute("SELECT * FROM `$table`");
			while($reg = $rs->FetchRow())
			{
				$sql .= "INSERT INTO `$table` VALUES(";
				for($i=0, $regsize=sizeof($reg); $i < $regsize; $i++)
				{
					$myreg = $reg[$i];
					switch($rs->FieldType($i))
					{
						case "string":
						case "datetime":
						case "unknown":
							$myreg = "'".mysql_real_escape_string($myreg)."'";
					}
					$sql .= $myreg.(($i < $regsize-1) ? ", " : ");\r\n");
				}
			}
		}
		return $sql;
	}
	function GetAccessData()
	{
		return array(
			"ip"=>$this->ip,
			"dbport"=>$this->dbport,
			"dbname"=>$this->dbname,
			"dbuser"=>$this->dbuser,
			"dbpass"=>$this->dbpass,
			);
	}
	function SetAccessData($data, $connect=false, $selectDb=true, $new=true)
	{
		$this->ip = $data["ip"];
		$this->dbport = $data["dbport"];
		$this->dbname = $data["dbname"];
		$this->dbuser = $data["dbuser"];
		$this->dbpass = $data["dbpass"];

		if($connect)
		{
			return $this->Connect($selectDb, $new);
		}

		return true;
	}
	/**
	 * Aplica htmlentities sobre todos los elementos de un array
	 * private
	 *
	 * @param array $array
	 */
	function array_htmlentities($array)
	{
		$keys = array_keys($array);
		foreach($keys as $key)
		{
			$array[$key] = htmlentities($array[$key]);
		}
		return $array;
	}
}
?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.