TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   unusual behavior (database class) (http://www.talkphp.com/absolute-beginners/4261-unusual-behavior-database-class.html)

allworknoplay 05-09-2009 11:57 PM

unusual behavior (database class)
 
I am getting an odd behavior with my database class. Here's what my fetch method looks like.


PHP Code:

public function fetch($fetch_type 'array') {
    
        
switch (
$fetch_type) {
        
case 
'array'
return 
mysql_fetch_array($this->results);
break;
            
case 
'assoc':
return 
mysql_fetch_assoc($this->results);
break;

case 
'object':
return 
mysql_fetch_object($this->results);
break;
}




In my main script. If I assign the object to a variable, I get the correct output. My table only has 1 record so I get this.


PHP Code:

$rows $db->fetch();
    
echo 
"Key: $rows[0] AND Value: $rows[1]"

My output is:

Key: 0 AND Value: Cleo


But if I do a foreach on the "$row" variable, I get a whole bunch of data...


PHP Code:

foreach ($rows AS $key => $value) {
    echo 
"Key: $key AND Value: $value <Br />";
    } 


My output is:

Quote:

Key: 0 AND Value: 0
Key: postalcode_id AND Value: 0
Key: 1 AND Value: cleo
Key: cat_name AND Value: cleo
Key: 2 AND Value: 90210
Key: postcode AND Value: 90210
Key: 3 AND Value: sold
Key: status AND Value: sold
Key: 4 AND Value:
Key: extra AND Value:

Shouldn't the output be just like the first output?
Just one row?


Key: 0 AND Value: Cleo ????

Also, the "mysql_num_rows" count comes back as 1, which is correct because there's only 1 record in the table..


ONE MORE THING....

I did a little troubleshooting...

When I do a count on "$rows":

$countme = count($rows);

I get back: 10


BUT, in my database class, I get just 1 back from this:


$count = $db->num_rows();

And the method looks like this:


PHP Code:

public function num_rows() {
    
return 
mysql_num_rows($this->results);
        



Enfernikus 05-10-2009 01:45 AM

It is a personal preference of mine, but I prefer to avoid switch statements when possible so I thought perhaps you would share this same bias and maybe prefer this way.

PHP Code:

public function fetch($fetch_type 'array')
{
    
$types = array(
                    
'array' => MYSQL_NUM,
                    
'assoc' => MYSQL_ASSOC,
                    
'both'    => MYSQL_BOTH
                  
);
     if( 
array_key_exists($fetch_type$types) )
     {
         return 
mysql_fetch_array($this->results$types[$fetch_type]);
     }



Wildhoney 05-10-2009 01:56 AM

That is the correct behaviour insofar as I can tell. You should really have a fetchRow and fetchAll function.

Having an ambiguous fetch function doesn't imply either one nor the other. That function would make a good fetchAll, but for a fetchRow, try using mysql_fetch_row and returning that.

Salathe 05-10-2009 10:57 AM

Quote:

Originally Posted by Wildhoney (Post 23926)
That is the correct behaviour insofar as I can tell. You should really have a fetchRow and fetchAll function.

Having an ambiguous fetch function doesn't imply either one nor the other. That function would make a good fetchAll, but for a fetchRow, try using mysql_fetch_row and returning that.

On the contrary, the method in the first post would constitute a fetchRow if you wanted to distinguish the two: mysql_fetch_array/assoc/object only fetch one row at a time.

Quote:

Originally Posted by allworknoplay (Post 23922)
I am getting an odd behavior with my database class...

The behaviour is as expected, if you know what those mysql_fetch_* functions do. By default, mysql_fetch_array will return an array with both number and associative indices. If you only want the number indices, provide the MYSQL_NUM constant to optional second argument, $result_type. You can call the same function with MYSQL_ASSOC to have only associative indices returned.

As for your "ONE MORE THING...." with regards to counting the return value of your method. You are counting the number of array items returned. Since the array only holds one row, the count is twice the number of columns (since both number and associative indices are being returned) in the table, not the number of rows returned by the query.

Wildhoney 05-10-2009 02:25 PM

Quote:

Originally Posted by Salathe (Post 23928)
On the contrary, the method in the first post would constitute a fetchRow if you wanted to distinguish the two: mysql_fetch_array/assoc/object only fetch one row at a time.

Well, of course, but I am thinking of Zend's implementation whereby you supply the query within the actual call to fetchRow and fetchAll, and the return is based on an actual item instead of many items contained within an array, so to speak.

allworknoplay 05-10-2009 03:49 PM

I guess I've been using fetch_array and fetch_assoc so much I kinda forgot how they worked exactly.

Here's how I normally access a table.

Hypothetical Table
table_id,name,type,status


So my "procedural" way of getting the data is like so:

PHP Code:

$results mysql_query("SELECT * FROM table");
$count mysql_num_rows($results);

for(
$i=0;$i<$count;<$i++) {

$rows mysql_fetch_array($results);
$table_id $rows['table_id'];
$name $rows['name'];
$type $rows['type'];
$status $rows['status'];

echo 
"$table_id$name$type$status <br />";



As you can see that's pretty straightforward. So I was hoping to accomplish the same thing the OO way....




$db->query("SELECT * FROM table");
$db->num_rows();
$db->fetch('row'); //pass 'row' as the parameter to access mysql_fetch_row function....

for($i=0;$i<$db->num_rows();<$i++) {

$rows = $db->fetch('row');
$table_id = $rows['table_id'];
$name = $rows['name'];
$type = $rows['type'];
$status = $rows['status'];

echo "$table_id, $name, $type, $status <br />";


DOESNT WORK-----^

}





So now how do I actually access the values in the returned rows like I did the procedural way?

Sorry, my transition from proc to OO is a little shaky...


All times are GMT. The time now is 07:45 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0