mysql_fetch_assoc devolver un valor entero
- Bogey
- Bogey


- Registrado: Jul 14, 2005
- Mensajes: 8211
- Loc: USA
- Status: Offline
Necesito ayuda...lo siguiente:
devuelve int (2)...?
¿Cómo es que es devolver un número entero?
Necesito ayuda.
PHP Código: [ Select ]
// Checking what type of result we want
if($type == 1)
{
while($row = mysql_fetch_assoc($results))
{
$result[] = $row;
}
}
elseif($type == 2)
{
while($row = mysql_fetch_array($results))
{
$result[] = $row;
}
}
elseif($type == 3)
{
while($row = mysql_fetch_object($results))
{
$result[] = $row;
}
}
elseif($type == 4)
{
while($row = mysql_fetch_row($results))
{
$result[] = $row;
}
}
else
{
while($row = mysql_fetch_assoc($results))
{
$result[] = $row;
}
}
if($type == 1)
{
while($row = mysql_fetch_assoc($results))
{
$result[] = $row;
}
}
elseif($type == 2)
{
while($row = mysql_fetch_array($results))
{
$result[] = $row;
}
}
elseif($type == 3)
{
while($row = mysql_fetch_object($results))
{
$result[] = $row;
}
}
elseif($type == 4)
{
while($row = mysql_fetch_row($results))
{
$result[] = $row;
}
}
else
{
while($row = mysql_fetch_assoc($results))
{
$result[] = $row;
}
}
- // Checking what type of result we want
- if($type == 1)
- {
- while($row = mysql_fetch_assoc($results))
- {
- $result[] = $row;
- }
- }
- elseif($type == 2)
- {
- while($row = mysql_fetch_array($results))
- {
- $result[] = $row;
- }
- }
- elseif($type == 3)
- {
- while($row = mysql_fetch_object($results))
- {
- $result[] = $row;
- }
- }
- elseif($type == 4)
- {
- while($row = mysql_fetch_row($results))
- {
- $result[] = $row;
- }
- }
- else
- {
- while($row = mysql_fetch_assoc($results))
- {
- $result[] = $row;
- }
- }
devuelve int (2)...?
¿Cómo es que es devolver un número entero?
Necesito ayuda.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Anonymous
- Bot


- Registrado: 25 Feb 2008
- Mensajes: ?
- Loc: Ozzuland
- Status: Online
Octubre 28th, 2010, 12:57 am
- joebert
- Sledgehammer


- Registrado: Feb 10, 2004
- Mensajes: 13455
- Loc: Florida
- Status: Offline
- Bogey
- Bogey


- Registrado: Jul 14, 2005
- Mensajes: 8211
- Loc: USA
- Status: Offline
Tengo el siguiente conjunto de funciones en mi clase:
Y si ejecuto el siguiente código:
Me sale el siguiente resultado:
Y mi caché dice
[EDIT:] ¿Cómo puedo evitar gedit de hacer las líneas adicionales en Ubuntu? Su llegando a ser muy irritante! [EDICIÓN EN LÍNEA:] (que fija las líneas adicionales se muestran para mejorar la legibilidad, pero gedit todavía lo hace.
PHP Código: [ Select ]
class db {
public function _get_($what, $from, $where = null)
{
// Generating the SQL for the query
$sql = array('SELECT' => $what,
'FROM' => $from);
// Generating the WHERE clause
if(!is_null($where))
{
if(is_array($where))
{
$where = $this->build_where($where, false);
// Merging the WHERE clause with the rest of the SQL array
$sql = array_merge($sql, array('WHERE' => $where));
}
}
// Setting the generated SQL to $sql
$sql = $this->build_key_query($sql);
// Setting the last sql to the correct variable
$this->last_sql = $sql;
// Retrieving the number of results there are to the query
$num = $this->num_rows($sql);
// Checking if the $num is greater than 1
if($num > 1)
{
$return = $this->fetch_rowset($sql);
}
else
{
$return = $this->fetch_row($sql);
}
// Returning the appropriate result (set).
return $return;
}
public function set_result_resource($sql = null, $return = false)
{
// Checking if we are connected to MySQL
if(!is_resource($this->mysql_link))
{
$this->connect();
}
// Checking if the SQL is empty
if(is_null($sql))
{
$sql = $this->get_last_sql();
}
// Sanitising the input
$sql = $this->san_query($sql);
// Getting the resource into a variable
$resource = mysql_query($sql);
// Setting the last result variable
$this->last_result = $resource;
// Checking if the result needs to be returned
if($return)
{
// Returning the SQL resource
return $resource;
}
else
{
// Checking if the result resource was created successfully
if($resource)
{
return true;
}
}
return false;
}
public function num_rows($sql, $type = 2)
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Setting the result resource
$results = $this->set_result_resource($sql, true);
// Getting the requested result
$result = (($type == 1) ? mysql_num_fields($results) : mysql_num_rows($results));
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
return $this->cache_results($sql, $result);
}
// Returning the result (if not cached)
return $result;
}
public function num_rows($sql, $type = 2)
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Setting the result resource
$results = $this->set_result_resource($sql, true);
// Getting the requested result
$result = (($type == 1) ? mysql_num_fields($results) : mysql_num_rows($results));
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
return $this->cache_results($sql, $result);
}
// Returning the result (if not cached)
return $result;
}
public function fetch_row($sql, $type = 1)
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Creating a result resource
$results = $this->set_result_resource($sql, true);
// Setting the result array
$result = array();
// Checking what type of result we want
if($type == 1)
{
$result[] = mysql_fetch_assoc($results);
}
elseif($type == 2)
{
$result[] = mysql_fetch_array($results);
}
elseif($type == 3)
{
$result[] = mysql_fetch_object($results);
}
elseif($type == 4)
{
$result[] = mysql_fetch_row($results);
}
else
{
$result[] = mysql_fetch_assoc($results);
}
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
return $this->cache_results($sql, $result);
}
// Returning the result (if not cached)
return $result;
}
}
public function _get_($what, $from, $where = null)
{
// Generating the SQL for the query
$sql = array('SELECT' => $what,
'FROM' => $from);
// Generating the WHERE clause
if(!is_null($where))
{
if(is_array($where))
{
$where = $this->build_where($where, false);
// Merging the WHERE clause with the rest of the SQL array
$sql = array_merge($sql, array('WHERE' => $where));
}
}
// Setting the generated SQL to $sql
$sql = $this->build_key_query($sql);
// Setting the last sql to the correct variable
$this->last_sql = $sql;
// Retrieving the number of results there are to the query
$num = $this->num_rows($sql);
// Checking if the $num is greater than 1
if($num > 1)
{
$return = $this->fetch_rowset($sql);
}
else
{
$return = $this->fetch_row($sql);
}
// Returning the appropriate result (set).
return $return;
}
public function set_result_resource($sql = null, $return = false)
{
// Checking if we are connected to MySQL
if(!is_resource($this->mysql_link))
{
$this->connect();
}
// Checking if the SQL is empty
if(is_null($sql))
{
$sql = $this->get_last_sql();
}
// Sanitising the input
$sql = $this->san_query($sql);
// Getting the resource into a variable
$resource = mysql_query($sql);
// Setting the last result variable
$this->last_result = $resource;
// Checking if the result needs to be returned
if($return)
{
// Returning the SQL resource
return $resource;
}
else
{
// Checking if the result resource was created successfully
if($resource)
{
return true;
}
}
return false;
}
public function num_rows($sql, $type = 2)
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Setting the result resource
$results = $this->set_result_resource($sql, true);
// Getting the requested result
$result = (($type == 1) ? mysql_num_fields($results) : mysql_num_rows($results));
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
return $this->cache_results($sql, $result);
}
// Returning the result (if not cached)
return $result;
}
public function num_rows($sql, $type = 2)
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Setting the result resource
$results = $this->set_result_resource($sql, true);
// Getting the requested result
$result = (($type == 1) ? mysql_num_fields($results) : mysql_num_rows($results));
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
return $this->cache_results($sql, $result);
}
// Returning the result (if not cached)
return $result;
}
public function fetch_row($sql, $type = 1)
{
// Checking if the SQL query was cached
if($this->cache_results === true)
{
$sql_file = $this->cache_dir . md5($sql) . '.cache';
// Checking if the SQL file exists
if(file_exists($sql_file))
{
return unserialize(file_get_contents($sql_file));
}
}
// Creating a result resource
$results = $this->set_result_resource($sql, true);
// Setting the result array
$result = array();
// Checking what type of result we want
if($type == 1)
{
$result[] = mysql_fetch_assoc($results);
}
elseif($type == 2)
{
$result[] = mysql_fetch_array($results);
}
elseif($type == 3)
{
$result[] = mysql_fetch_object($results);
}
elseif($type == 4)
{
$result[] = mysql_fetch_row($results);
}
else
{
$result[] = mysql_fetch_assoc($results);
}
// Checking if we need to free the result resource
if($this->free_result == true)
{
$this->free_result();
}
// Checking if we need to cache the result
if($this->cache_results === true)
{
return $this->cache_results($sql, $result);
}
// Returning the result (if not cached)
return $result;
}
}
- class db {
- public function _get_($what, $from, $where = null)
- {
- // Generating the SQL for the query
- $sql = array('SELECT' => $what,
- 'FROM' => $from);
- // Generating the WHERE clause
- if(!is_null($where))
- {
- if(is_array($where))
- {
- $where = $this->build_where($where, false);
- // Merging the WHERE clause with the rest of the SQL array
- $sql = array_merge($sql, array('WHERE' => $where));
- }
- }
- // Setting the generated SQL to $sql
- $sql = $this->build_key_query($sql);
- // Setting the last sql to the correct variable
- $this->last_sql = $sql;
- // Retrieving the number of results there are to the query
- $num = $this->num_rows($sql);
- // Checking if the $num is greater than 1
- if($num > 1)
- {
- $return = $this->fetch_rowset($sql);
- }
- else
- {
- $return = $this->fetch_row($sql);
- }
- // Returning the appropriate result (set).
- return $return;
- }
- public function set_result_resource($sql = null, $return = false)
- {
- // Checking if we are connected to MySQL
- if(!is_resource($this->mysql_link))
- {
- $this->connect();
- }
- // Checking if the SQL is empty
- if(is_null($sql))
- {
- $sql = $this->get_last_sql();
- }
- // Sanitising the input
- $sql = $this->san_query($sql);
- // Getting the resource into a variable
- $resource = mysql_query($sql);
- // Setting the last result variable
- $this->last_result = $resource;
- // Checking if the result needs to be returned
- if($return)
- {
- // Returning the SQL resource
- return $resource;
- }
- else
- {
- // Checking if the result resource was created successfully
- if($resource)
- {
- return true;
- }
- }
- return false;
- }
- public function num_rows($sql, $type = 2)
- {
- // Checking if the SQL query was cached
- if($this->cache_results === true)
- {
- $sql_file = $this->cache_dir . md5($sql) . '.cache';
- // Checking if the SQL file exists
- if(file_exists($sql_file))
- {
- return unserialize(file_get_contents($sql_file));
- }
- }
- // Setting the result resource
- $results = $this->set_result_resource($sql, true);
- // Getting the requested result
- $result = (($type == 1) ? mysql_num_fields($results) : mysql_num_rows($results));
- // Checking if we need to free the result resource
- if($this->free_result == true)
- {
- $this->free_result();
- }
- // Checking if we need to cache the result
- if($this->cache_results === true)
- {
- return $this->cache_results($sql, $result);
- }
- // Returning the result (if not cached)
- return $result;
- }
- public function num_rows($sql, $type = 2)
- {
- // Checking if the SQL query was cached
- if($this->cache_results === true)
- {
- $sql_file = $this->cache_dir . md5($sql) . '.cache';
- // Checking if the SQL file exists
- if(file_exists($sql_file))
- {
- return unserialize(file_get_contents($sql_file));
- }
- }
- // Setting the result resource
- $results = $this->set_result_resource($sql, true);
- // Getting the requested result
- $result = (($type == 1) ? mysql_num_fields($results) : mysql_num_rows($results));
- // Checking if we need to free the result resource
- if($this->free_result == true)
- {
- $this->free_result();
- }
- // Checking if we need to cache the result
- if($this->cache_results === true)
- {
- return $this->cache_results($sql, $result);
- }
- // Returning the result (if not cached)
- return $result;
- }
- public function fetch_row($sql, $type = 1)
- {
- // Checking if the SQL query was cached
- if($this->cache_results === true)
- {
- $sql_file = $this->cache_dir . md5($sql) . '.cache';
- // Checking if the SQL file exists
- if(file_exists($sql_file))
- {
- return unserialize(file_get_contents($sql_file));
- }
- }
- // Creating a result resource
- $results = $this->set_result_resource($sql, true);
- // Setting the result array
- $result = array();
- // Checking what type of result we want
- if($type == 1)
- {
- $result[] = mysql_fetch_assoc($results);
- }
- elseif($type == 2)
- {
- $result[] = mysql_fetch_array($results);
- }
- elseif($type == 3)
- {
- $result[] = mysql_fetch_object($results);
- }
- elseif($type == 4)
- {
- $result[] = mysql_fetch_row($results);
- }
- else
- {
- $result[] = mysql_fetch_assoc($results);
- }
- // Checking if we need to free the result resource
- if($this->free_result == true)
- {
- $this->free_result();
- }
- // Checking if we need to cache the result
- if($this->cache_results === true)
- {
- return $this->cache_results($sql, $result);
- }
- // Returning the result (if not cached)
- return $result;
- }
- }
Y si ejecuto el siguiente código:
PHP Código: [ Select ]
var_dump($db->_get_('*','test'));
Me sale el siguiente resultado:
Quote:
int (2)
Y mi caché dice
Quote:
i: 2;
[EDIT:] ¿Cómo puedo evitar gedit de hacer las líneas adicionales en Ubuntu? Su llegando a ser muy irritante! [EDICIÓN EN LÍNEA:] (que fija las líneas adicionales se muestran para mejorar la legibilidad, pero gedit todavía lo hace.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Bogey
- Bogey


- Registrado: Jul 14, 2005
- Mensajes: 8211
- Loc: USA
- Status: Offline
- righteous_trespasser
- Scuffle


- Registrado: Mar 12, 2007
- Mensajes: 6228
- Loc: South-Africa
- Status: Offline
- Bogey
- Bogey


- Registrado: Jul 14, 2005
- Mensajes: 8211
- Loc: USA
- Status: Offline
Yo había vuelto a instalar apache phpmyadmin, y mysql, así que perdí esa base de datos, pero he creado otra, y esto se dice int (4).
Parece que todo su hacer es contar el número de filas que hay...
Código: [ Select ]
--
-- Table structure for table `test`
--
CREATE TABLE IF NOT EXISTS `test` (
`test_field_1` varchar(20) NOT NULL,
`test_field_2` varchar(20) NOT NULL,
`test_field_3` varchar(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `test`
--
INSERT INTO `test` (`test_field_1`, `test_field_2`, `test_field_3`) VALUES
('field content 3.1', 'field content 3.2', 'field content 3.3'),
('field content 4.1', 'field content 4.2', 'field content 4.3'),
('field content 1.1', 'field content 1.2', 'field content 1.3'),
('field content 2.1', 'field content 2.2', 'field content 2.3');
-- Table structure for table `test`
--
CREATE TABLE IF NOT EXISTS `test` (
`test_field_1` varchar(20) NOT NULL,
`test_field_2` varchar(20) NOT NULL,
`test_field_3` varchar(20) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
--
-- Dumping data for table `test`
--
INSERT INTO `test` (`test_field_1`, `test_field_2`, `test_field_3`) VALUES
('field content 3.1', 'field content 3.2', 'field content 3.3'),
('field content 4.1', 'field content 4.2', 'field content 4.3'),
('field content 1.1', 'field content 1.2', 'field content 1.3'),
('field content 2.1', 'field content 2.2', 'field content 2.3');
- --
- -- Table structure for table `test`
- --
- CREATE TABLE IF NOT EXISTS `test` (
- `test_field_1` varchar(20) NOT NULL,
- `test_field_2` varchar(20) NOT NULL,
- `test_field_3` varchar(20) NOT NULL
- ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
- --
- -- Dumping data for table `test`
- --
- INSERT INTO `test` (`test_field_1`, `test_field_2`, `test_field_3`) VALUES
- ('field content 3.1', 'field content 3.2', 'field content 3.3'),
- ('field content 4.1', 'field content 4.2', 'field content 4.3'),
- ('field content 1.1', 'field content 1.2', 'field content 1.3'),
- ('field content 2.1', 'field content 2.2', 'field content 2.3');
Parece que todo su hacer es contar el número de filas que hay...
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
- Bogey
- Bogey


- Registrado: Jul 14, 2005
- Mensajes: 8211
- Loc: USA
- Status: Offline
Ok, haciendo una investigación poco más en esto, me di cuenta de cuál es el problema.
El problema no fue con el PHP para obtener los resultados...el problema estaba en la caché.
Por alguna razón el problema se resuelve...No tengo idea de lo que pasó. Se fija después de poner var_dump ($ resultado); en la función de caché ($ resultado es el contenido que se almacena en caché)...después de que estaba trabajando perfectamente.
De todos modos, estoy contento de haber sido capaz de que te lo arreglen.
El problema no fue con el PHP para obtener los resultados...el problema estaba en la caché.
Por alguna razón el problema se resuelve...No tengo idea de lo que pasó. Se fija después de poner var_dump ($ resultado); en la función de caché ($ resultado es el contenido que se almacena en caché)...después de que estaba trabajando perfectamente.
De todos modos, estoy contento de haber sido capaz de que te lo arreglen.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
Página 1 de 1
Para responder a este tema que necesita para ingresar o registrarse. Es gratis.
Publicar Información
- Total de mensajes en este tema: 7 mensajes
- Usuarios navegando por este Foro: No hay usuarios registrados visitando el Foro y 143 invitados
- No puede abrir nuevos temas en este Foro
- No puede responder a temas en este Foro
- No puede editar sus mensajes en este Foro
- No puede borrar sus mensajes en este Foro
- No puede enviar adjuntos en este Foro
