View Single Post
Old 05-09-2009, 11:57 PM   #1 (permalink)
allworknoplay
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default 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);
        

allworknoplay is offline  
Reply With Quote