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 04-03-2008, 02:00 PM   #1 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default Getting maaad :P

So, seriously.. what's wrong with this function?

php Code:
public function getPoems($status = null) {
           
            if($status == 'active') {
               
                $sql = sprintf("    SELECT
                                        *
                                    FROM
                                        `%s`
                                    WHERE
                                        `%s` = 'active'
                                    ORDER BY
                                        `%s`
                                    ASC"
,
                                       
                                    $this->db->table['poems'],
                                    $this->db->col['poem_status'],
                                    $this->db->col['poem_order']);
                                   
            }
           
            elseif($status == null) {
               
                $sql = sprintf("    SELECT
                                        *
                                    FROM
                                        `%s`
                                    ORDER BY
                                        `%s`
                                    ASC"
,
                                       
                                    $this->db->table['poems'],
                                    $this->db->col['poem_order']);
               
            }
                               
            $query = $this->db->query($sql);
           
            if($query) {
               
                while($results = mysql_fetch_array($query)) {
                   
                    $array[] = $results;
                   
                }
               
                return $array;
               
            }
           
            return false;
           
        }

php Code:
$poemlist = $tanaxia['poems']->getPoems();

    foreach($poemlist as $a) {
       
        foreach($a as $b) {
           
            echo $b;
            echo '<br />';
           
        }
       
    }

echoes:
Quote:
1
1
active
active
1
1
Test
Test
I'm just testing this poem..
I'm just testing this poem..
gpwgjwpegwepgjwpegjwpgwoejgpwegjwpeg
gpwgjwpegwepgjwpegjwpgwoejgpwegjwpeg
YES, it echoes 2 of everything, but there only really exists 1 entry in the database.

So WHY oh WHY does it echo out 2 of everything?
Something's messed up with that array that the getPoems() function returns =//
__________________
Tanax is offline  
Reply With Quote
Old 04-03-2008, 04:17 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

Most likely, this problem is with your use of mysql_fetch_array (PHP Manual page). You don't provide any value for the second argument (result_type), so it falls back to the default which is MYSQL_BOTH (other allowed values are MYSQL_ASSOC and MYSQL_NUM). Because of this, the fetched array will contain both the numeric (the column number) and associative (the column name) keys into the result.

To fix the behaviour for your needs, you need to put in one of MYSQL_NUM/ASSOC as the second argument -- most likely MYSQL_ASSOC for most normal usage.

Just to get an idea of what's going on for your own learning, try the following with using the different constants and see what they return. You'll understand clearly if you do that.
PHP Code:
$poemlist $tanaxia['poems']->getPoems();
print_r($poemlist); 
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Tanax (04-03-2008)
Old 04-03-2008, 06:06 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
Most likely, this problem is with your use of mysql_fetch_array (PHP Manual page). You don't provide any value for the second argument (result_type), so it falls back to the default which is MYSQL_BOTH (other allowed values are MYSQL_ASSOC and MYSQL_NUM). Because of this, the fetched array will contain both the numeric (the column number) and associative (the column name) keys into the result.

To fix the behaviour for your needs, you need to put in one of MYSQL_NUM/ASSOC as the second argument -- most likely MYSQL_ASSOC for most normal usage.

Just to get an idea of what's going on for your own learning, try the following with using the different constants and see what they return. You'll understand clearly if you do that.
PHP Code:
$poemlist $tanaxia['poems']->getPoems();
print_r($poemlist); 
Yes, it works now
Thanks
I already tried with print_r and var_dump but none of them made sense to me at the time

Anyways, it's strange, because I don't have to do this on all my mysql_fetch_array queries =//
__________________
Tanax 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


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

 
     

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