OOP Problem

Discussion in 'PHP' started by Koncept, Jul 14, 2006.

  1. #1
    wasy to tired and stressed to think can someone please help out
    Error:
    Fatal error: Call to undefined method waDatabase::executeQuery() in V:\xampp\htdocs\webArts\webArts\lib\db\db.php on line 75
    Code (markup):
    I know what to do to fix it just remove "$db->executeQuery('SELECT * FORM test');" but thats not what i want to do i want that to actually run the executeQuery from mysqlConnection

    
    <?php
    define ('DB_BASE_DIR', dirname(__FILE__));
    if(!defined('DS')){
    	define('DS', DIRECTORY_SEPARATOR);
    }
    class waDatabase {
    	function connect($info){
    		static $connections = array();
    		if(isset($connections[$name])){
    			return $connections[$name];
    		}
    		$name = (isset($info['name'])) ? $info['name'] : 'default';
    		$info['driver'] = strtolower($info['driver']); // For those who like Caps...
    		if(!isset($info['driver'])){ // Was there a driver provided?
    			return trigger_error('No database driver specified', E_USER_ERROR);
    		}
    		$driver = DB_BASE_DIR . DS . 'drivers' . DS . $info['driver'] . '_driver.php';
    		$class = $info['driver'] . 'Connection';
    		if(!is_readable($driver)){ // Does the driver file exist?
    			return trigger_error('Database driver does not exist: ' . $info['driver']  . '_driver.php', E_USER_ERROR);
    		}
    		require_once($driver); // Require the database driver.
    		if(!class_exists($class)){ // Does the driver file contain the correct class?
    			return trigger_error('Database driver does not exist: ' . $info['driver']  . '_driver.php', E_USER_ERROR);
    		}
    		$dba = new $class();
    		if(!is_a($dba, 'waDbConnection')){ // Does the class extend waDbConnection?
    			return trigger_error('Database driver does not extend waDbConnection: ' . $info['driver']  . '_driver.php', E_USER_ERROR);
    		}
    		// Error is thrown in the constructor (hopefully).
    		$dba->open($info);
    		$connections[$name] = $dba;
    		return $connections[$name];
    	}
    	function open($name){
    		return waDatabase::connect(array('name' => $name));
    	}
    }
    class waDbConnection {
    	var $_dbinfo;
    	function waDbConnection(){
    		//assert(FALSE);
    	}
    	function close(){
    		assert(FALSE);
    	}
    	function connect($dbinfo){
    		assert(FALSE);
    	}
    	function executeQuery($query){
    		assert(FALSE);
    	}
    	function getRow($stmt, $args = array()){
    		$result = $this->executeQuery($stmt, $args);
    		$result->next();
    		return $result->current();
    	}
    	function open($dbinfo) {
    		if($ret = $this->connect($dbinfo)){
    			$this->_dbinfo = $dbinfo;
    		}
    		return $ret;
    	}
    }
    
    $db = new waDatabase;
    $info = array(
    'driver' => 'MySQL',
    'function' => 'mysql_connect',
    'host' => 'localhost',
    'user' => 'labs',
    'password' => '******,
    'database' => 'labs');
    $db->connect($info);
    $db->executeQuery('SELECT * FORM test');
    ?>
    
    Code (markup):
    and drivres/mysql_driver.php
    
    <?php
    class mysqlConnection extends waDbConnection{
    	var $_link;
    	function mysqlConnection(){
    		parent::waDbConnection();
    		if(!extension_loaded('mysql')){
    			trigger_error('The MySQL extension is not loaded.', E_USER_ERROR);
    		}
    	}
    	function connect($dbinfo){
    		if(is_array($dbinfo)){
    			switch($dbinfo['function']){
    				case 'mysql_connect';
    					mysql_connect($dbinfo['host'], $dbinfo['user'], $dbinfo['password']) or trigger_error(mysql_error(),E_USER_ERROR);
    					break;
    				case 'mysql_pconnect';
    					mysql_pconnect($dbinfo['host'], $dbinfo['user'], $dbinfo['password']) or trigger_error(mysql_error(),E_USER_ERROR);
    					break;
    				default:
    					trigger_error('No driver function or Invalid driver function supplied.',E_USER_ERROR);
    				}
    			mysql_select_db($dbinfo['database']) or trigger_error(mysql_error(),E_USER_ERROR);
    			$this->_link = true;
    		}
    	}
    	function close(){
    		$this->isConnect();
    		mysql_close();
    		return true;
    	}
    	function executeQuery($query){
    		$this->isConnect();
    		mysql_query($query);
    		return true;
    	}
    	function getInsertId() {
    		return mysql_insert_id();
    	}
    	function isConnect(){
    		if(!$this->_link){
    			trigger_error("Error: Not connected", E_USER_ERROR);
    		}
    	}
    }
    ?>
    
    Code (markup):

     
    Koncept, Jul 14, 2006 IP
  2. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #2
    $db->executeQuery('SELECT * FORM test');

    you have "FORM" instead of "FROM"
     
    smatts9, Jul 14, 2006 IP
  3. Koncept

    Koncept Peon

    Messages:
    50
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thats another error but its not the error that im getting that would just be a query error im getting a inheritence error
    Fatal error: Call to undefined method waDatabase::executeQuery() in V:\xampp\htdocs\webArts\webArts\lib\db\db.php on line 75
     
    Koncept, Jul 14, 2006 IP
  4. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,
    This may not be relevant but you should use:

    $db = new waDatabase();
    PHP:
    instead of just
    $db = new waDatabase;
    PHP:
    I dunno if thats whats causing the errors, but thats how an object is created in all the PHP scripts I have seen. See this

    http://in2.php.net/new

    And here:
    
    function executeQuery($query){
    $this->isConnect();
    mysql_query($query);
    return true;
    }
    
    PHP:
    You may want to return the result of the query too,in order to use the data you get from the query. Just a thought...
    Hope that helped,
    Thomas
     
    coderlinks, Jul 22, 2006 IP
  5. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #5
    There's no inheritance going on here.

    Is this your code or from somewhere else? A quick glance shows it to be rather full of errors.

    I think what you want is:
    $connection = $db->connect($info);
    $connection->executeQuery('SELECT * FROM test');
     
    jnestor, Jul 22, 2006 IP
  6. DrMalloc

    DrMalloc Peon

    Messages:
    130
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    waDatabase HAS no executeQuery method, I think that's the problem. The method is in waDbConnection. There might be a missing extend problem, as jnestor said.
     
    DrMalloc, Jul 22, 2006 IP