TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-09-2009, 11:57 PM   #1 (permalink)
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
Old 05-10-2009, 01:45 AM   #2 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

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]);
     }

Enfernikus is offline  
Reply With Quote
Old 05-10-2009, 01:56 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-10-2009, 10:57 AM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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 View Post
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.
Salathe is offline  
Reply With Quote
Old 05-10-2009, 02:25 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-10-2009, 03:49 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

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...
allworknoplay is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
base classes..... allworknoplay Absolute Beginners 16 05-10-2009 08:09 PM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
[Tutorial] Basic tutorial about class basics Tanax Absolute Beginners 14 07-24-2008 01:37 PM
PHP5 Classes A to Z Part 1 quantumkangaroo Advanced PHP Programming 11 04-01-2008 04:21 AM
Tutorial: PHP and OOP, a beginners guide Village Idiot Tips & Tricks 0 09-06-2007 04:23 PM


All times are GMT. The time now is 02:02 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design