Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-25-2009, 02:25 AM   PM User | #1
byte1bit
New Coder

 
Join Date: Feb 2007
Location: Lisboa
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
byte1bit is an unknown quantity at this point
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
byte1bit is offline   Reply With Quote
Old 12-25-2009, 03:19 AM   PM User | #2
byte1bit
New Coder

 
Join Date: Feb 2007
Location: Lisboa
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
byte1bit is an unknown quantity at this point
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'); 
byte1bit is offline   Reply With Quote
Old 12-27-2009, 03:48 AM   PM User | #3
byte1bit
New Coder

 
Join Date: Feb 2007
Location: Lisboa
Posts: 21
Thanks: 3
Thanked 0 Times in 0 Posts
byte1bit is an unknown quantity at this point
well, i guess i'll just put all mysql related stuff in a single class. at least i can reuse the same connection.
byte1bit is offline   Reply With Quote
Old 01-13-2010, 01:34 PM   PM User | #4
dominicdinada
New Coder

 
Join Date: Dec 2009
Posts: 32
Thanks: 1
Thanked 3 Times in 3 Posts
dominicdinada is an unknown quantity at this point
misread the question

Last edited by dominicdinada; 01-13-2010 at 01:36 PM.. Reason: misread the question
dominicdinada is offline   Reply With Quote
Old 01-13-2010, 01:39 PM   PM User | #5
dominicdinada
New Coder

 
Join Date: Dec 2009
Posts: 32
Thanks: 1
Thanked 3 Times in 3 Posts
dominicdinada is an unknown quantity at this point
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
dominicdinada is offline   Reply With Quote
Reply

Bookmarks

Tags
mysql, oop, php, pos_mysqloriginalresult

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:38 AM.


Advertisement
Log in to turn off these ads.