Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2007
    Location
    Lisboa
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question how does the mysqli results get passed?

    hi ppl. i'm doing a tutorial from PHP Object-Oriented Solutions, a david powers book ( http://www.amazon.com/PHP-Object-Oriented-Solutions-David-Powers/dp/1430210117/ref=sr_1_3?ie=UTF8&s=books&qid=1261679189&sr=8-3 )

    in pg 296 there is a method to connect to mysqli, in class Pos_MysqlOriginalConnection:

    PHP Code:
        public function getResultSet($sql) {
          
    $results = new Pos_MysqlOriginalResult($sql$this->_connection);
          return 
    $results;
        } 
    the class Pos_MysqlOriginalResult() has the following definition:

    PHP Code:
        public function __construct($sql$connection) {
            if (!
    $this->_result mysql_query($sql$connection)) {
                throw new 
    Exception(mysql_error($connection) . '. The actual query submitted was: ' $sql);
            }
        } 
    this class works, and i get the results, with all the tables. what troubles me is that i don't understand why, because they become stored in "this->_result", inside Pos_MysqlOriginalResult(), and i don't see how the property $results gets that data.

    if i could inderstand that, i could extend the class Pos_MysqlOriginalResult, to perform several database related operations. Can anyone shed some light into this, please?
    Last edited by byte1bit; 12-25-2009 at 02:55 AM. Reason: forgot to say please

  • #2
    New Coder
    Join Date
    Feb 2007
    Location
    Lisboa
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    maybe it's overkill, but here is the original code:

    ResultadosSQL:

    PHP Code:
    <?php 
    include_once("autoload.php");

    class 
    ResultadosSQL implements IteratorCountable {
        protected 
    $_current// Stores value of current element.
        
    protected $_key// Stores current element key.
        
    protected $_valid// Determines whether a current element exists.
        
    protected $_result// Stores the database result.
        
        /**
         * Uses a MySQLi connection to submit a query and stores the result in the _result property.
         * 
         * @param object $sql         query sql
         * @param object $connection  ligação a BD
         * @return void
         */
        
    public function __construct($sql$connection) {
            if (!
    $this->_result $connection->query($sql)) {
                throw new 
    RuntimeException($connection->error.'. The actual query submitted was: '.$sql);
            }
        }
        
    // Gets next row from database result and increments key.
        
    public function next() {
            
    $this-> _current $this->_result->fetch_assoc();
            
    $this-> _valid is_null($this->_current) ? false true;
            
    $this-> _key++;
        }
        
        
    // Returns value of current element.
        
    public function current() {
            return 
    $this->_current;
        }
        
        
    //Returns key of current element.
        
    public function key() {
            return 
    $this-> _key;
        }
        
        
    // Determines whether there is current element.
        
    public function valid() {
            return 
    $this->_valid;
        }
        
        
    //Moves to first row of database result.
        
    public function rewind() {
            if (!
    is_null($this->_key)) {
                
    $this->_result->data_seek(0);
            }
            
    $this->_key 0;
            
    $this->_current $this->_result->fetch_assoc();
            
    $this->_valid is_null($this->_current) ? false true;
        }
        
        
    //Returns number of rows in database result.
        
    public function count() {
            return 
    $this->_result->num_rows;
        }
    }
    LigacaoSql:

    PHP Code:
    <?php 
    include_once("autoload.php");
    /** 
    * criar 1a ligação mysqli

    * @author lsag
    * @access public  
    * @example teste.php
    */ 
    class LigacaoSql {
        
    /**
         * MySQLi connection
         *
         * @var mysqli  Database connection using MySQL Improved.
         * @access public $_connection
         */
        
    protected $_connection;
        
        
    /**
         * Creates a database connection using MySQL Improved.
         *
         * @param string $host  Database server name.
         * @param string $user  Database user account.
         * @param string $pwd   User account password.
         * @param string $db    Name of database to connect to. */
        
    public function __construct ($host$user$pwd$db) {
            
    $this->_connection = @ new mysqli ($host$user$pwd$db);
            if (
    mysqli_connect_errno ()) {
                throw new 
    RuntimeException ('Cannot access database: '.mysqli_connect_error ());
            }
        }
        
    /**
         * Submits query to database and returns result as Pos_MysqlImprovedResult object.
         *
         * @param string $sql               SQL query.
         * @return ResultadosSQL  Result of query. */
        
    public function getResultSet ($sql) {
            
    $results = new ResultadosSQL ($sql$this->_connection);
            return 
    $results;
        }
        
        
    //  Closes the database connection when object is destroyed.
        
    public function __destruct () {
            
    $this->_connection->close ();
        }
    }
    usage example:
    PHP Code:
    $conn = new LigacaoSql ('localhost''movimento''xampp''mar');
    $result $conn->getResultSet ('SELECT id, titulo, local, data, bandas  FROM locais'); 

  • #3
    New Coder
    Join Date
    Feb 2007
    Location
    Lisboa
    Posts
    21
    Thanks
    3
    Thanked 0 Times in 0 Posts
    well, i guess i'll just put all mysql related stuff in a single class. at least i can reuse the same connection.

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    32
    Thanks
    1
    Thanked 3 Times in 3 Posts
    misread the question
    Last edited by dominicdinada; 01-13-2010 at 01:36 PM. Reason: misread the question

  • #5
    New Coder
    Join Date
    Dec 2009
    Posts
    32
    Thanks
    1
    Thanked 3 Times in 3 Posts
    While it doesnt specify I am pretty sure that he is using the Code Igniter Framework in conjunction with the book if so, There are built in Database commands for basic commands. ../system/database/
    The other folder to look at would be in the application folder


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •