dam errors

Discussion in 'PHP' started by liam_d, Dec 9, 2005.

  1. #1
    Well im trying to use a bit of this OOP. But its not working too well...

    Im trying to get my forum to connect to the test database...

    I have this on my config file;
    
    // EmoBlue Copyright 2005 Liam Dawe
    // database host, username, password, database
    $config_file["host"] = "localhost";
    $config_file["username"] = "root";
    $config_file["password"] = "";
    $config_file["database"] = "mysql";
    $config_file['sql_db'] = "mysql";
    
    Code (markup):
    And then this in my settings file;
    
    // Get the config
    include('./config.php');
    
    if ($config_file['sql_db'] == "mysql")
    {
    	// Get the database file
    	include('includes/database/mysql.php');
    	$database = new mysql();
    	$database->database_connect($config_file['host'], $config_file['user'], $config_file['pass'], $config_file['host'], $config_file['database']);
    }
    
    Code (markup):
    And then this is the actual class and function to connect;
    
    class mysql
    {
    	// Connect  to the database
    	function database_connect($db_host, $db_user, $db_pass, $db)
    	{
      mysql_connect($db_host, $db_user, $db_pass);
      mysql_select_db($db);
    	}
    }
    
    Code (markup):
    It says
    I am using easyPHP on my machine to test, just wondering if its a problem with easyphp, my code or possibly because its on windows?

    I think actually it has to be my code, the way ive done it as my installer connects fine when using traditional connecting;
    
      mysql_connect($_POST['host'], $_POST['user'], $_POST['pass']) or die(mysql_error());
      mysql_select_db($_POST['database']) or die(mysql_error());
    
    Code (markup):
    That works fine yet my code for the forum connecting itself using OOP doesn't? Can someone please point me to the errors?
     
    liam_d, Dec 9, 2005 IP
  2. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    it isn't using a password.
     
    onlyican.com, Dec 9, 2005 IP
  3. liam_d

    liam_d Active Member

    Messages:
    306
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Doesn't need one im using root on my local machine.
     
    liam_d, Dec 9, 2005 IP
  4. abuzant

    abuzant Well-Known Member

    Messages:
    956
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #4
    Hello,

    To go straight, it is not reading your config.php file.

    Am i right?
    Place a line saying (echo "oops";) at the head of your config.php and run the script, you will notice that it is not shown. Why? because it is not included.

    Possible reasons:
    php_opendir restriction

    Try the following instead..
    
    include($_SERVER["DOCUMENT_ROOT"] . "config.php");
    
    PHP:
    Wish U luck.
     
    abuzant, Dec 9, 2005 IP
  5. liam_d

    liam_d Active Member

    Messages:
    306
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Well i got it to work, that wasn't actually the problem, i put user & pass instead of username & password in the bit after i include the config file.

    But now it says this;

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\program files\easyphp1-8\www\includes\settings.php on line 39

    and that code is this;
    
    // Setup the config, find the name and value from config table and then make the name = to value and stick it into $site_config
    $result = mysql_query("SELECT `name`, `value` FROM `config`"); 
                
    $site_config = array(); 
    				
    while ($array = mysql_fetch_array($result)) 
    { 
    	$name = $array['name']; 
    	$value = $array['value']; 
    	$site_config[$name] = $value; 
    }
    
    
    PHP:
    Any help would be ace!
     
    liam_d, Dec 9, 2005 IP
  6. abuzant

    abuzant Well-Known Member

    Messages:
    956
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    140
    #6
    Look, i assume this must be something you did not expect.. but i am posting one small class library that will help you from now on till the day you forget about the internet.. it is the library i use in all my 150 online sites, and btw, it works ;)

    Here isthe dbconn.php file:
    
    <?php
    class DB_base { 
        // get server/instance 
        function setinst($instance){ 
            $this->instance = $instance; 
        } 
    
        // get username 
        function setuser($user){ 
            $this->user = $user; 
        } 
    
        // get password 
        function setpass($pass){ 
            $this->pass = $pass; 
        } 
    
        // get database name 
        function dbname($dbname){ 
            $this->dbname = $dbname; 
        } 
    
        // Use persistent connections? 
        // Only call this if you want persistent connections 
        function persist(){ 
            $this->persist = 1; 
        } 
    
    } 
    
    
    
    class DB extends DB_base { 
        // Connect or pconnect. 
        function connect(){ 
            if($this->persist){ 
                $this->conn = 
                mysql_pconnect($this->instance, $this->user, $this->pass); 
            } else { 
                $this->conn =   
                mysql_connect($this->instance, $this->user, $this->pass); 
            } 
            mysql_select_db($this->dbname, $this->conn); 
            return($this->conn); 
        } 
    
        // close 
        function close(){ 
            if($this->persist) { 
                $ret = 1; 
            } else { 
                $ret = mysql_close($this->conn); 
            } 
            return($ret); 
        } 
    
        // query 
        function query($query){ 
            $this->result = mysql_query($query); 
            return($this->result); 
        } 
                      
        // numrows 
        function numrows(){ 
            $this->numrows = mysql_num_rows($this->result); 
            return($this->numrows); 
        } 
                      
        // insertid
        function insertid(){ 
            $this->insertid = mysql_insert_id($this->conn); 
            return($this->insertid); 
        } 
    
        // affected rows 
        function affrows(){ 
            $this->affrows = mysql_affected_rows($this->result); 
            return($this->affrows); 
        } 
    
        // seek 
        function seek($row){ 
            $seek = mysql_data_seek($this->result, $row); 
            return($seek); 
        } 
    
        // fetch object 
        function fobject(){ 
            $object = mysql_fetch_object($this->result); 
            return($object); 
        } 
    
        // fetch array 
        function farray(){ 
            $array = mysql_fetch_array($this->result); 
            return($array); 
        } 
    
        // free 
        function free(){ 
            $free = mysql_free_result($this->result); 
            return($free); 
        } 
    } 
    
    
    
    //	 Initiate Database
    function initDB()
    {
    	global $dbtype;
    	$db = new DB;
    	$db->setinst("localhost");
                 // are we on localhost or online server - my tweak
    	if($_SERVER["SERVER_ADDR"] == "127.0.0.1")
    	{
                              // i am deleoping @home
    		$db->setuser("root");
    		$db->setpass("");
    		$db->dbname("[b]DATABASE-NAME[/b]");
    	} else {
                             // we are now online
    		$db->setuser("[b]USER-NAME[/b]");
    		$db->setpass("[b]PASSWORD[/b]");
    		$db->dbname("[b]DATABASE-NAME[/b]");
    	}
    
    	$db->persist(); 
    	$conn = $db->connect();
    	if(strlen(mysql_error()))
    	{
    		echo mysql_error();
    	}
    	return $db;
    }
    
    
    ?>
    
    PHP:
    Now, here are the usage instructions...

    Anytime you want to use that, you need to include it, simply do a
    include('dbconn.php');
    PHP:
    Step 1: Connecto To Database
    In one line, you can simply connecto to your database as follows ..
    
    if(!$db) $db = initDB();
    
    PHP:
    Step 2: Querying The Database
    
    $query = "SELECT ............";
    $db->query($query) or die(mysql_error());
    
    PHP:
    Step 3: Getting Results
    
    $result = $db->farray()
    
    PHP:
    OR
    
    $result = $db->fobject()
    
    PHP:
    And as you have the $result now, it is your choise to do whatever you like with it ;)

    If this helps anybody or you just find it useful, please give a good rating to the post..

    Cya there someday..
    bye
     
    abuzant, Dec 9, 2005 IP