mysql_fetch_assoc devolver un valor entero

  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8211
  • Loc: USA
  • Status: Offline

Nota Octubre 28th, 2010, 12:57 am

Necesito ayuda...lo siguiente:
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;
    }
}
  1. // Checking what type of result we want
  2. if($type == 1)
  3. {
  4.     while($row = mysql_fetch_assoc($results))
  5.     {
  6.         $result[] = $row;
  7.     }
  8. }
  9. elseif($type == 2)
  10. {
  11.     while($row = mysql_fetch_array($results))
  12.     {
  13.         $result[] = $row;
  14.     }
  15. }
  16. elseif($type == 3)
  17. {
  18.     while($row = mysql_fetch_object($results))
  19.     {
  20.         $result[] = $row;
  21.     }
  22. }
  23. elseif($type == 4)
  24. {
  25.     while($row = mysql_fetch_row($results))
  26.     {
  27.         $result[] = $row;
  28.     }
  29. }
  30. else
  31. {
  32.     while($row = mysql_fetch_assoc($results))
  33.     {
  34.         $result[] = $row;
  35.     }
  36. }


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
  • No Avatar
  • Registrado: 25 Feb 2008
  • Mensajes: ?
  • Loc: Ozzuland
  • Status: Online

Nota Octubre 28th, 2010, 12:57 am

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Registrado: Feb 10, 2004
  • Mensajes: 13455
  • Loc: Florida
  • Status: Offline

Nota Octubre 28th, 2010, 4:11 am

¿Cuál es devolver un número entero?
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8211
  • Loc: USA
  • Status: Offline

Nota Octubre 28th, 2010, 10:19 am

Tengo el siguiente conjunto de funciones en mi clase:
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;
    }
}
  1. class db {
  2.     public function _get_($what, $from, $where = null)
  3.     {
  4.         // Generating the SQL for the query
  5.         $sql = array('SELECT' => $what,
  6.                      'FROM' => $from);
  7.        
  8.         // Generating the WHERE clause
  9.         if(!is_null($where))
  10.         {
  11.             if(is_array($where))
  12.             {
  13.                 $where = $this->build_where($where, false);
  14.                
  15.                 // Merging the WHERE clause with the rest of the SQL array
  16.                 $sql = array_merge($sql, array('WHERE' => $where));
  17.             }
  18.         }
  19.        
  20.         // Setting the generated SQL to $sql
  21.         $sql = $this->build_key_query($sql);
  22.        
  23.         // Setting the last sql to the correct variable
  24.         $this->last_sql = $sql;
  25.        
  26.         // Retrieving the number of results there are to the query
  27.         $num = $this->num_rows($sql);
  28.        
  29.         // Checking if the $num is greater than 1
  30.         if($num > 1)
  31.         {
  32.             $return = $this->fetch_rowset($sql);
  33.         }
  34.         else
  35.         {
  36.             $return = $this->fetch_row($sql);
  37.         }
  38.        
  39.         // Returning the appropriate result (set).
  40.         return $return;
  41.     }
  42.  
  43.     public function set_result_resource($sql = null, $return = false)
  44.     {
  45.         // Checking if we are connected to MySQL
  46.         if(!is_resource($this->mysql_link))
  47.         {
  48.             $this->connect();
  49.         }
  50.        
  51.         // Checking if the SQL is empty
  52.         if(is_null($sql))
  53.         {
  54.             $sql = $this->get_last_sql();
  55.         }
  56.        
  57.         // Sanitising the input
  58.         $sql = $this->san_query($sql);
  59.        
  60.         // Getting the resource into a variable
  61.         $resource = mysql_query($sql);
  62.        
  63.         // Setting the last result variable
  64.         $this->last_result = $resource;
  65.        
  66.         // Checking if the result needs to be returned
  67.         if($return)
  68.         {
  69.             // Returning the SQL resource
  70.             return $resource;
  71.         }
  72.         else
  73.         {
  74.             // Checking if the result resource was created successfully
  75.             if($resource)
  76.             {
  77.                 return true;
  78.             }
  79.         }
  80.         return false;
  81.     }
  82.  
  83.     public function num_rows($sql, $type = 2)
  84.     {
  85.         // Checking if the SQL query was cached
  86.         if($this->cache_results === true)
  87.         {
  88.             $sql_file = $this->cache_dir . md5($sql) . '.cache';
  89.        
  90.             // Checking if the SQL file exists
  91.             if(file_exists($sql_file))
  92.             {
  93.                 return unserialize(file_get_contents($sql_file));
  94.             }
  95.         }
  96.        
  97.         // Setting the result resource
  98.         $results = $this->set_result_resource($sql, true);
  99.        
  100.         // Getting the requested result
  101.         $result = (($type == 1) ? mysql_num_fields($results) : mysql_num_rows($results));
  102.        
  103.         // Checking if we need to free the result resource
  104.         if($this->free_result == true)
  105.         {
  106.             $this->free_result();
  107.         }
  108.        
  109.         // Checking if we need to cache the result
  110.         if($this->cache_results === true)
  111.         {
  112.             return $this->cache_results($sql, $result);
  113.         }
  114.        
  115.         // Returning the result (if not cached)
  116.         return $result;
  117.     }
  118.  
  119.     public function num_rows($sql, $type = 2)
  120.     {
  121.         // Checking if the SQL query was cached
  122.         if($this->cache_results === true)
  123.         {
  124.             $sql_file = $this->cache_dir . md5($sql) . '.cache';
  125.        
  126.             // Checking if the SQL file exists
  127.             if(file_exists($sql_file))
  128.             {
  129.                 return unserialize(file_get_contents($sql_file));
  130.             }
  131.         }
  132.        
  133.         // Setting the result resource
  134.         $results = $this->set_result_resource($sql, true);
  135.        
  136.         // Getting the requested result
  137.         $result = (($type == 1) ? mysql_num_fields($results) : mysql_num_rows($results));
  138.        
  139.         // Checking if we need to free the result resource
  140.         if($this->free_result == true)
  141.         {
  142.             $this->free_result();
  143.         }
  144.        
  145.         // Checking if we need to cache the result
  146.         if($this->cache_results === true)
  147.         {
  148.             return $this->cache_results($sql, $result);
  149.         }
  150.        
  151.         // Returning the result (if not cached)
  152.         return $result;
  153.     }
  154.  
  155.     public function fetch_row($sql, $type = 1)
  156.     {
  157.         // Checking if the SQL query was cached
  158.         if($this->cache_results === true)
  159.         {
  160.             $sql_file = $this->cache_dir . md5($sql) . '.cache';
  161.        
  162.             // Checking if the SQL file exists
  163.             if(file_exists($sql_file))
  164.             {
  165.                 return unserialize(file_get_contents($sql_file));
  166.             }
  167.         }
  168.        
  169.         // Creating a result resource
  170.         $results = $this->set_result_resource($sql, true);
  171.        
  172.         // Setting the result array
  173.         $result = array();
  174.        
  175.         // Checking what type of result we want
  176.         if($type == 1)
  177.         {
  178.             $result[] = mysql_fetch_assoc($results);
  179.         }
  180.         elseif($type == 2)
  181.         {
  182.             $result[] = mysql_fetch_array($results);
  183.         }
  184.         elseif($type == 3)
  185.         {
  186.             $result[] = mysql_fetch_object($results);
  187.         }
  188.         elseif($type == 4)
  189.         {
  190.             $result[] = mysql_fetch_row($results);
  191.         }
  192.         else
  193.         {
  194.             $result[] = mysql_fetch_assoc($results);
  195.         }
  196.        
  197.         // Checking if we need to free the result resource
  198.         if($this->free_result == true)
  199.         {
  200.             $this->free_result();
  201.         }
  202.        
  203.         // Checking if we need to cache the result
  204.         if($this->cache_results === true)
  205.         {
  206.             return $this->cache_results($sql, $result);
  207.         }
  208.        
  209.         // Returning the result (if not cached)
  210.         return $result;
  211.     }
  212. }


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
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8211
  • Loc: USA
  • Status: Offline

Nota Octubre 31st, 2010, 8:18 pm

Así que, ¿hay algo que puedo hacer para arreglar esto? Im tipo de pegado en cuanto a qué hacer a continuación.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • righteous_trespasser
  • Scuffle
  • Genius
  • Avatar de Usuario
  • Registrado: Mar 12, 2007
  • Mensajes: 6228
  • Loc: South-Africa
  • Status: Offline

Nota Noviembre 1st, 2010, 6:13 am

lo que hace la tabla `test` parece?
Let's leave all our *plum* where it is and go live in the jungle ...
  • Bogey
  • Bogey
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8211
  • Loc: USA
  • Status: Offline

Nota Noviembre 1st, 2010, 10:55 am

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).

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');
  1. --
  2. -- Table structure for table `test`
  3. --
  4. CREATE TABLE IF NOT EXISTS `test` (
  5.  `test_field_1` varchar(20) NOT NULL,
  6.  `test_field_2` varchar(20) NOT NULL,
  7.  `test_field_3` varchar(20) NOT NULL
  8. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  9. --
  10. -- Dumping data for table `test`
  11. --
  12. INSERT INTO `test` (`test_field_1`, `test_field_2`, `test_field_3`) VALUES
  13. ('field content 3.1', 'field content 3.2', 'field content 3.3'),
  14. ('field content 4.1', 'field content 4.2', 'field content 4.3'),
  15. ('field content 1.1', 'field content 1.2', 'field content 1.3'),
  16. ('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
  • Genius
  • Avatar de Usuario
  • Registrado: Jul 14, 2005
  • Mensajes: 8211
  • Loc: USA
  • Status: Offline

Nota Noviembre 1st, 2010, 11:09 am

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.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8

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
 
 

© 2011 Unmelted, LLC. Ozzu® es una marca registrada de Unmelted, LLC