MySQLi - Multiple Rows Not Working?

Discussion in 'PHP' started by Pos1tron, Oct 11, 2008.

  1. #1
    I'm changing my main database classes to use mysqli, and I've hit a snag - this should be returning two different rows - both with the Name column value of London, but with completely different information (eg: CountryCode of one is CAN, the other GBR) - but instead it's just returning the same row. (This is using the World demo database from mysql.com.)

    After spending several hours working on this, I'm pretty sure I'm missing something obvious, but either way I'm willing to pay for a fix if necessary.

    Anyway, here's the:
    • Wrong Output:
    Array
    (
        [0] => Array
            (
                [City] => Array
                    (
                        [ID] => 1820
                        [Name] => London
                        [CountryCode] => CAN
                        [District] => Ontario
                        [Population] => 339917
                    )
    
            )
    
        [1] => Array
            (
                [City] => Array
                    (
                        [ID] => 1820
                        [Name] => London
                        [CountryCode] => CAN
                        [District] => Ontario
                        [Population] => 339917
                    )
    
            )
    
    )
    Array
    (
        [Open] => Array
            (
                [Query] => 0
                [Conn] => 0
            )
    
        [Total] => Array
            (
                [Query] => 1
                [Conn] => 1
            )
    
    )
    
    HTML:
    • Correct (written out myself) Output it should give (I'm not sure of the order of the two however):
    Array
    (
        [0] => Array
            (
                [City] => Array
                    (
                        [ID] => 456
                        [Name] => London
                        [CountryCode] => GBR
                        [District] => England
                        [Population] => 7285000
                    )
    
            )
    
        [1] => Array
            (
                [City] => Array
                    (
                        [ID] => 1820
                        [Name] => London
                        [CountryCode] => CAN
                        [District] => Ontario
                        [Population] => 339917
                    )
    
            )
    
    )
    Array
    (
        [Open] => Array
            (
                [Query] => 0
                [Conn] => 0
            )
    
        [Total] => Array
            (
                [Query] => 1
                [Conn] => 1
            )
    
    )
    
    HTML:
    • PHP Code (the DB Class referenced is, as far as this class is concerned, just mysqli):
    <?php
    
    class DB extends mysqli {
        ##################################################################
        #    VARIABLES OF CLASS
        ##################################################################
        
        ##################################################################
        #    Passed into Class @ start
        #    >    $DB_Host - Database Host
        #    >    $DB_User - Database Username
        #    >    $DB_Pass - Database Password
        #    >    $DB_Name - Database Name
        ##################################################################
            private $DB_Host = 'localhost';
            private $DB_User = 'root';
            private $DB_Pass = '';
            private $DB_Name = 'World';
        
        ##################################################################
        #    Created in Class
        #    >    $this->DB_Stats - Records Operations upon DB
        ##################################################################
            private $DB_Stats;
        
        
        ##################################################################
        #    FUNCTIONS OF CLASS
        ##################################################################
        
        public function __construct($Host = 'localhost', $User = 'root', $Pass = '', $Name = 'World') {
            $this->DB_Stats = array(
                'Open' => array(
                    'Query' => 0,
                    'Conn' => 0
                ),
                'Total' => array(
                    'Query' => 0,
                    'Conn' => 0
                )
            );
            
            $this->DB_Host = $Host;
            $this->DB_User = $User;
            $this->DB_Pass = $Pass;
            $this->DB_Name = $Name;
            parent::__construct($this->DB_Host, $this->DB_User, $this->DB_Pass, $this->DB_Name);
            $this->Stats_Mod('Open', 'Conn', 1);
        }
        
        public function Stats_Mod($Cat = 'Total', $Stat = 'Query', $Num = 1) {
            if($Num == 1) {
                $this->DB_Stats[$Cat][$Stat] += 1;
                if($Cat == 'Open') {
                    $this->DB_Stats['Total'][$Stat] += 1;
                }
            } else {
                $this->DB_Stats[$Cat][$Stat] -= 1;
            }
        }
        
        public function Stats_Get() {
            return $this->DB_Stats;
        }
        
        public function Close() {
            $this->Stats_Mod('Open', 'Conn', 0);
        }
        
        public function __destruct() {
            $this->Close();
        }
    }
    
    class DB_Query_Select {
        ##################################################################
        #    VARIABLES OF CLASS
        ##################################################################
        
        ##################################################################
        #    Passed into Class @ start
        #    >    $this->DB - DB Instance for running queries (by reference)
        #    >    $this->SQL - The actual SQL for Querying
        #    >    $this->Params - The Params (as an array) for bind_param
        ##################################################################
            private $DB;
            private $SQL;
            private $Params;
        
        ##################################################################
        #    Created in Class
        #    >    $this->Engine - Instance of prepare
        #    >    $this->Meta - The result_metadata() of the above
        #    >    $this->Row - The Result from the current row
        #    >    $this->Return - Full Query Result (as 'objectified array')
        ##################################################################
            private $Engine;
            private $Meta;
            private $Row;
            private $Return;
    
        
        ##################################################################
        #    FUNCTIONS OF CLASS
        ##################################################################
        
        public function __construct(&$DB, $SQL, $Params = array()) {
            ##################################################################
            #    First, prepare the DB instance and config data for later use.
            ##################################################################
                $this->DB         =     $DB;
                $this->SQL         =    $SQL;
                $this->Params     =     $Params;
            
            try {
                ##################################################################
                #    $this->Start() prepares the query, and that's about it. It's 
                #    just wrapped up for no real reason in a cotty candy layer...
                ##################################################################
                    $this->Start();
                
                ##################################################################
                #    $this->Run() both binds the parameters, runs the query, and 
                #    binds the result. It does not, however, return it - that it 
                #    part of the job of $this->End().
                ##################################################################
                    $this->Run();
                
                ##################################################################
                #    $this->End() closes down the query, and then returns the 
                #    result, usually to where used in the DB Class this is called.
                ##################################################################
                    $this->End();
            } catch(Exception $e) {
                ##################################################################
                #    DB_Exception is not yet implemented. Instead this just returns 
                #    the error info for development - this will change once the 
                #    DB_Exception class is complete/ready for use.
                ##################################################################
                    if(class_exists('DB_Exception')) {
                        throw new DB_Exception();
                    } else {
                        var_dump($e->getMessage());
                    }
            }
        }
        
        public function Start() {
            $this->Engine = $this->DB->prepare($this->SQL);
            $this->DB->Stats_Mod('Open', 'Query', 1);
        }
        
        public function Run() {
            $this->Run_Bind_Params();
            $this->Run_Execute();
            $this->Run_Bind_Result();
        }
        
        public function Run_Bind_Params() {
            $Function = array(
                &$this->Engine, 
                'bind_param'
            );
            call_user_func_array($Function, $this->Params);
        }
        
        public function Run_Execute() {
            $this->Engine->execute();
            $this->Meta = $this->Engine->result_metadata();
        }
        
        public function Run_Bind_Result() {
            $Function = array(
                &$this->Engine, 
                'bind_result'
            );
            while($Column = $this->Meta->fetch_field()) {
                $Params[] = &$this->Row[$Column->table][$Column->name];
            }
            call_user_func_array($Function, $Params);
        }
        
        public function End() {
            while($this->Engine->fetch()) {
                $this->Return[] = $this->Row;
            }
            $this->Engine->close();
            $this->DB->Stats_Mod('Open', 'Query', 0);
        }
        
        public function Get() {
            return $this->Return;
        }
    }
    
    $DB = new DB;
    $DB_Query_Select = new DB_Query_Select(
        $DB,
        'SELECT *, `CountryCode` FROM `City` WHERE `Name` = ? LIMIT 0, 2', 
        array(
            's', 
            $_GET['City']
        )
    );
    print_r($DB_Query_Select->Get());
    $DB->Close();
    print_r($DB->Stats_Get());
    unset($DB_Query_Select);
    
    ?>
    PHP:
    Any ideas?
     
    Pos1tron, Oct 11, 2008 IP
  2. Pos1tron

    Pos1tron Peon

    Messages:
    95
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    nvm, fixed.
     
    Pos1tron, Oct 12, 2008 IP