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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts

    OOP database objects

    Ok so I have an SQL class that I made myself and has worked for me so far. I am now trying to use two SEPARATE connections and it seems to be failing badly and I don't know why.

    Shouldn't I be able to create two new objects each with it's own DB connection and each one maintain as an object? Using PHP5 by the way.

    The SQL class is:

    PHP Code:
    class sql {
        
        static 
    $query;
        static 
    $return;
        static 
    $numrows;
        static 
    $result;
        static 
    $db;
        
        function 
    __construct $dbhost$dbuser$dbpass$dbname ) {
            if (
    is_object($this)) {
                
    $this->query '';
                
    $this->return '';
                
    $this->numrows 0;
                
    $this->result '';
                
    $this->db mysql_connect($dbhost$dbuser$dbpass) or die ('Cannot connect to database.');
                
    mysql_select_db($dbname$this->db);
            } else {
                
    self::$query '';
                
    self::$return '';
                
    self::$numrows 0;
                
    self::$result '';
                
    self::$db mysql_connect($dbhost$dbuser$dbpass) or die ('Cannot connect to database.');
                
    mysql_select_db($dbnameself::$db);
            }
        }
        
        function 
    close () {
            if (
    is_object($this)) {
                
    mysql_close($this->db);
            } else {
                
    mysql_close(self::$db);
            }
        }
        
        
    // === Function to log MySQL problems ===
        
    function logger $qry$err '' ) {
            
            
    $qry addslashes($qry);
            
    $err addslashes($err);
            
    makeLog("Error in MySQL query. The Query was: \"$qry\". The error MySQL gave was: $err");
                
        }
        
        function 
    error $qry$err$ret ) {
            
            if (
    is_object($this)) {
                
    $this->logger($qry$err);
            } else {
                
    self::logger($qry$err);
            }
            
            if(
    $ret == 1) {
                die(
    'Error in SQL: '.$err.' - Query: '.$qry);
            } else if (
    $ret == 2) {
                echo 
    'Transaction Error';
            }
            
        }
        
        function 
    update $qry$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;        
            
    $return mysql_query($qry);
            if(!
    $return) {
                if (
    is_object($this)) {
                    
    $this->error($qrymysql_error(), $ret);
                } else {
                    
    self::error($qrymysql_error(), $ret);
                }
            } else {
                if(
    $ret == 3) {
                    echo 
    '<font color="green">Successful Transaction..</font><br>';
                }
            }
            
        }
        
        function 
    updateA $data$items$where '' ) {
            global 
    $db$dbg$debug;
            
            if(
    is_array($items)) {
                foreach(
    $items as $key => $value) {
                    
    $ups[] = $key." = '".sanSQL($value)."'";
                }
                
                
    $ups implode(', '$ups);
                
                
    $qry "UPDATE $data SET $ups $where";
                
                if (
    is_object($this)) {
                    
    $this->insert($qry);
                } else {
                    
    self::insert($qry);
                }
            } else {
                echo 
    'We need the data fields to insert';
            }
            
        }
        
        function 
    insert $qry$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;        
            
    $return mysql_query($qry);
            if(!
    $return) {
                if (
    is_object($this)) {
                    
    $this->error($qrymysql_error(), $ret);
                } else {
                    
    self::error($qrymysql_error(), $ret);
                }
            } else {
                if(
    $ret == 3) {
                    echo 
    '<font color="green">Successful Transaction..</font><br>';
                }
            }
            
        }
        
        function 
    insertA $data$items ) {
            global 
    $db$dbg$debug;
            
            if(
    is_array($items)) {
                foreach(
    $items as $key => $value) {
                    
    $rows[] = $key;
                    
    $vals[] = "'".sanSQL($value)."'";
                }
                
                
    $rows implode(', '$rows);
                
    $vals implode(', '$vals);
                
                
    $qry "INSERT INTO $data ($rows) VALUES ($vals)";
                
                if (
    is_object($this)) {
                    
    $this->insert($qry);
                } else {
                    
    self::insert($qry);
                }
            } else {
                echo 
    'We need the data fields to insert';
            }
            
        }
        
        function 
    remove $qry$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;        
            
    $return mysql_query($qry);
            if(!
    $return) {
                if (
    is_object($this)) {
                    
    $this->error($qrymysql_error(), $ret);
                } else {
                    
    self::error($qrymysql_error(), $ret);
                }
            } else {
                if(
    $ret == 3) {
                    echo 
    '<font color="green">Successful Transaction..</font><br>';
                }
            }
            
        }
        
        function 
    singleSel $qry$type 1$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;
            
    $final = @mysql_query($qry);
            if(@
    mysql_num_rows($final) > 0) {
                
    $return = @mysql_result($final,0,0);
            } else {
                
    $return '';
            }
            
            if(!
    $return && $return '') {
                if (
    is_object($this)) {
                    
    $this->error($qrymysql_error(), $ret);
                } else {
                    
    self::error($qrymysql_error(), $ret);
                }
            } else {
                if(
    $type == 1) {
                    return 
    $return;
                } else if(
    $type == 2) {
                    echo 
    $return;
                }
            }
            
        }
        
        function 
    rowSelect $qry$type 1$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;
            
    $final mysql_query($qry);
            if(
    mysql_num_rows($final) > 0) {
                
    $return mysql_fetch_array($final);
            } else {
                
    $return '';
            }
            
            if(!
    $return && $return '') {
                if (
    is_object($this)) {
                    
    $this->error($qrymysql_error(), $ret);
                } else {
                    
    self::error($qrymysql_error(), $ret);
                }
            } else {
                if(
    $type == 1) {
                    return 
    $return;
                } else if(
    $type == 2) {
                    echo 
    implode(';'$return);
                }
            }
            
        }
        
        function 
    rowSelectRet $qry$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;
            
    $return mysql_query($qry);
            if(!
    $return) {
                if (
    is_object($this)) {
                    
    $this->error($qrymysql_error(), $ret);
                } else {
                    
    self::error($qrymysql_error(), $ret);
                }
            } else {
                
                if (
    is_object($this)) {
                    
    $this->numrows mysql_num_rows($return);
                } else {
                    
    self::$numrows mysql_num_rows($return);
                }
                
                while(
    $row mysql_fetch_array($return)) {
                    
    $result[] = $row;
                }
                
                return 
    $result;
            }
            
        }
        
        function 
    getRows () {
            
            if (
    is_object($this)) {
                    return 
    $this->numrows;
                } else {
                    return 
    self::$numrows;
                }
                
        }
        
        function 
    getLastId () {
            
            return 
    mysql_insert_id();
                
        }
        
        

    Like I said it ALL works fine, the problem is now when I try to create two separate connections to two different DBs. For some reason after the second db is connected and a new object made for it any call to DB1 works off of DB2. Let me show a bit of code.

    PHP Code:
    $db1 = new sql($dbhost$dbuser$dbpass$dbname);
    $dbs $db1->rowSelect("SELECT * FROM table WHERE id = {$_POST[id]}"); 
    That works fine, then DB2:

    PHP Code:
    $db1 = new sql($dbhost$dbuser$dbpass$dbname);
    $db2 = new sql($dbs[5], $dbs[6], $dbs[7], $dbs[4]);
    $dbs $db1->rowSelect("SELECT * FROM table WHERE id = {$_POST[id]}"); 
    This gives me an error, it can't find the table "table" under the DB for con2. So for some reason even though I am referencing $db1 in my query it is still trying to connect using $db2.

    Any ideas?
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    So much is wrong.

    $this will always be an object.

    You are declaring properties as static (static $db;), but then referencing them as both static (self::$db) as well as non-static ($this->db).

    And I haven't even gotten to the debugging yet.

    If you're not seeing like 100 notices being thrown, you must be using PHP4, have all errors suppressed, or both.

    To allow multiple connections, drop all the static stuff.
    Last edited by kbluhm; 11-13-2011 at 04:36 PM.

  • #3
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    That is because during my code I use them as sql::function more than I use them as an object. Hence why I check if it's an object or not before I call it.

    I'm actually not getting any error's at all, I am using PHP5 and I have E_ALL in error's turned on since it's my Dev environment. I have been using this code for 3 or 4 system's now with out problems.

    If I don't declare var's as static, what do I declare them as?
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You cannot (should not) have it both ways. If you declare a function as public, you should not access it as static... and vice versa.

    I just copied the class locally, tried connecting to a local DB, and here is my result:
    PHP Code:
    error_reportingE_ALL E_STRICT );
    ini_set'display_errors');

    /* sql class code */

    $sql = new sql'localhost''root''''kbluhm_local' ); 
    Output:
    Code:
    <br />
    <b>Strict Standards</b>:  Accessing static property sql::$query as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>16</b><br />
    <br />
    <b>Strict Standards</b>:  Accessing static property sql::$return as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>17</b><br />
    <br />
    <b>Strict Standards</b>:  Accessing static property sql::$numrows as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>18</b><br />
    <br />
    <b>Strict Standards</b>:  Accessing static property sql::$result as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>19</b><br />
    <br />
    <b>Strict Standards</b>:  Accessing static property sql::$db as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>20</b><br />
    <br />
    <b>Strict Standards</b>:  Accessing static property sql::$db as non static in <b>C:\WAMP\www\local.cobaltdev.org\public_html\index.php</b> on line <b>21</b><br />

  • #5
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Hmm, well on my end I have zero warnings, so not sure. At any rate, what do I change the $db variable to then to get it to keep separate connections? I have tried both private and protected and it still has the same problem.
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    I'm not going to rewrite your entire class for you, but here is an example to get you pointed in the right direction. I just whipped this up, so typos may exist, but they shouldn't be major.
    PHP Code:
    class sql
    {

        private 
    $db;

        public function 
    __construct$host$user$pass$name )
        {

            if ( ! 
    $this->db mysql_connect$host$user$pass ) )
            {
                throw new 
    Exception(
                    
    'Unable to connect to ' $host
                
    );
            }

            if ( ! 
    mysql_select_db$dbname$this->db ) )
            {
                throw new 
    Exception(
                    
    'Unable to select ' $name
                
    );
            }

        }

        public function 
    __destruct()
        {
            
    $this->close();
        }

        public function 
    close()
        {
            return 
    mysql_close$this->db );
        }

        public function 
    query$query )
        {
            return new 
    sql_result$query$this->db );
        }

    }

    class 
    sql_result
    {

        private 
    $result$rowCount 0;

        public function 
    __construct$querysql $db )
        {
            
    $this->result   mysql_query$query$db );
            
    $this->rowCount = (
                
    === stripostrim$query ), 'SELECT' ) ||
                
    === stripostrim$query ), 'SHOW' )
            ) ? 
    mysql_num_rows$this->result ) : mysql_affected_rows$db );
        }

        public function 
    free()
        {
            return 
    mysql_free_result$this->result );
        }

        public function 
    rowCount()
        {
            return 
    $this->rowCount;
        }

        public function 
    rewind()
        {
            return 
    $this->seek);
        }

        public function 
    seek$i )
        {
            return 
    mysql_data_seek$this->result$i );
        }

        public function 
    fetch()
        {
            return 
    mysql_fetch_assoc$this->result );
        }

        public function 
    fetchAll()
        {

            
    $this->rewind();
            
    $rows = array();

            while ( 
    $row $this->fetch() )
            {
                
    $rows[] = $row;
            }

            return 
    $rows;

        }


    Notice two classes: sql and sql_result. It bugs me somewhat when people will "abstract" the connection, but not the result.
    PHP Code:
    $sql = new sql'localhost''root''''test_db' );

    $res $sql->query'
        SELECT *
        FROM `test_table`
        WHERE `test_datetime` > NOW()
        LIMIT 10
    );

    if ( ! 
    $res->rowCount() )
    {
        echo 
    'Nothing found!';
    }

    // then
    foreach ( $res->fetchAll() as $row )
    {
        
    var_dump$row );
    }

    // or
    while ( $row $res->fetch() )
    {
        
    var_dump$row );

    Edit:
    In the above code, I would never access sql::query(); because it has been declared as non-static. To gracefully handle the PHP4-era syntax in your original code, PHP5 made itself backward-compatible in that not declaring static assumes public, as well as not declaring protected/private assumes public. So defining a method like so:
    PHP Code:
    function foo$bar )
    {
        return 
    $bar;

    ...is identical to:
    PHP Code:
    public function foo$bar )
    {
        return 
    $bar;

    ...which should not be called statically.
    Last edited by kbluhm; 11-13-2011 at 05:29 PM.

  • #7
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Thank you, but I really wasn't asking anyone to re-create my class for me, as you can see by my original post I was asking where I could fix the multiple DB problem in the current class. I like my class, and it works for me and my use, but thank you once more, I don't think I'll be switching.

    To be honest the class you posted added 3 extra steps for me that are not needed and can be handled class side, while with mine it's quick and simple and returns the array immediately when I call my row function, hence WHY I have multiple functions for each thing. But thanks.

    I have changed the $db variable to private and contruct to public, but still have the same problems in my class. I have also in the rowSelectRet function changed mysql_query to:

    PHP Code:
    mysql_query($qry$this->db); 
    But still same problem. As I understand OOP, each time I create a NEW object it should all maintain a NEW Object. So why are the two mixing themselves?

    Also when I try your Destruct function I get this error:

    Warning: mysql_close(): 5 is not a valid MySQL-Link resource in libraries\sql.inc.php on line 26
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by kbluhm View Post
    I'm not going to rewrite your entire class for you, but here is an example to get you pointed in the right direction. I just whipped this up, so typos may exist, but they shouldn't be major.


    I didn't say I was rewriting yours. I did, however, say I did not want to rewrite yours, and was providing an untested example to serve as more of a demo in proper syntax.

    If you're trying to make multiple connections, and it's not allowing it... then something is in fact not working fine. Were I to take on a project with this class, the first thing I would do is refactor it from scratch.

    You cannot have it both ways. Either the method is static or it is not. You should not be calling (for example!) $sql->query() in one place, and sql::query() in another without making complex usage of the __call() and __callStatic() magic methods... but we're not even going to get into that now because that type of usage would be entirely ridiculous.

    Quote Originally Posted by jfreak53 View Post
    As I understand OOP, each time I create a NEW object it should all maintain a NEW Object. So why are the two mixing themselves?
    I'm not so sure you fully understand OOP as far as static properties and methods go. The two are being mixed because you are seemingly randomly interpolating static and non-static method and property calls, as well as declaring properties as static that evidently should not be. There is just so much wrong with that code, I contemplated just moving on and not saying anything at all.
    Last edited by kbluhm; 11-13-2011 at 05:53 PM.

  • #9
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Ok, for the sake of argument and I need to find out what is wrong here ha ha

    I removed all self:: instances and removed everything else like that. Now pure OOP, still doesn't work

    This is my new code:

    PHP Code:
    class sql {
        
        private 
    $query;
        private 
    $return;
        private 
    $numrows;
        private 
    $result;
        private 
    $db;
        
        public function 
    __construct $dbhost$dbuser$dbpass$dbname ) {
                
    $this->query '';
                
    $this->return '';
                
    $this->numrows 0;
                
    $this->result '';
                
    $this->db mysql_connect($dbhost$dbuser$dbpass) or die ('Cannot connect to database.');
                
    mysql_select_db($dbname$this->db);
        }
        
        
    /*public function __destruct() {
            $this->close();
        }*/
        
        
    function close () {
            
    mysql_close($this->db);
        }
        
        
    // === Function to log MySQL problems ===
        
    function logger $qry$err '' ) {
            
            
    $qry addslashes($qry);
            
    $err addslashes($err);
            
    makeLog("Error in MySQL query. The Query was: \"$qry\". The error MySQL gave was: $err");
                
        }
        
        function 
    error $qry$err$ret ) {
            
            
    $this->logger($qry$err);
            
            if(
    $ret == 1) {
                die(
    'Error in SQL: '.$err.' - Query: '.$qry);
            } else if (
    $ret == 2) {
                echo 
    'Transaction Error';
            }
            
        }
        
        function 
    update $qry$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;        
            
    $return mysql_query($qry);
            if(!
    $return) {
                
    $this->error($qrymysql_error(), $ret);
            } else {
                if(
    $ret == 3) {
                    echo 
    '<font color="green">Successful Transaction..</font><br>';
                }
            }
            
        }
        
        function 
    updateA $data$items$where '' ) {
            global 
    $db$dbg$debug;
            
            if(
    is_array($items)) {
                foreach(
    $items as $key => $value) {
                    
    $ups[] = $key." = '".sanSQL($value)."'";
                }
                
                
    $ups implode(', '$ups);
                
                
    $qry "UPDATE $data SET $ups $where";
                
                
    $this->insert($qry);
            } else {
                echo 
    'We need the data fields to insert';
            }
            
        }
        
        function 
    insert $qry$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;        
            
    $return mysql_query($qry);
            if(!
    $return) {
                
    $this->error($qrymysql_error(), $ret);
            } else {
                if(
    $ret == 3) {
                    echo 
    '<font color="green">Successful Transaction..</font><br>';
                }
            }
            
        }
        
        function 
    insertA $data$items ) {
            global 
    $db$dbg$debug;
            
            if(
    is_array($items)) {
                foreach(
    $items as $key => $value) {
                    
    $rows[] = $key;
                    
    $vals[] = "'".sanSQL($value)."'";
                }
                
                
    $rows implode(', '$rows);
                
    $vals implode(', '$vals);
                
                
    $qry "INSERT INTO $data ($rows) VALUES ($vals)";
                
                
    $this->insert($qry);
            } else {
                echo 
    'We need the data fields to insert';
            }
            
        }
        
        function 
    remove $qry$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;        
            
    $return mysql_query($qry);
            if(!
    $return) {
                
    $this->error($qrymysql_error(), $ret);
            } else {
                if(
    $ret == 3) {
                    echo 
    '<font color="green">Successful Transaction..</font><br>';
                }
            }
            
        }
        
        function 
    singleSel $qry$type 1$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;
            
    $final = @mysql_query($qry);
            if(@
    mysql_num_rows($final) > 0) {
                
    $return = @mysql_result($final,0,0);
            } else {
                
    $return '';
            }
            
            if(!
    $return && $return '') {
                
    $this->error($qrymysql_error(), $ret);
            } else {
                if(
    $type == 1) {
                    return 
    $return;
                } else if(
    $type == 2) {
                    echo 
    $return;
                }
            }
            
        }
        
        function 
    rowSelect $qry$type 1$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;
            
    $final mysql_query($qry);
            if(
    mysql_num_rows($final) > 0) {
                
    $return mysql_fetch_array($final);
            } else {
                
    $return '';
            }
            
            if(!
    $return && $return '') {
                
    $this->error($qrymysql_error(), $ret);
            } else {
                if(
    $type == 1) {
                    return 
    $return;
                } else if(
    $type == 2) {
                    echo 
    implode(';'$return);
                }
            }
            
        }
        
        function 
    rowSelectRet $qry$ret ) {
            global 
    $db$dbg$debug;
            
            
    $dbg[] = $qry;
            
    $return mysql_query($qry$this->db);
            if(!
    $return) {
                
    $this->error($qrymysql_error(), $ret);
            } else {
                
                
    $this->numrows mysql_num_rows($return);
                
                while(
    $row mysql_fetch_array($return)) {
                    
    $result[] = $row;
                }
                
                return 
    $result;
            }
            
        }
        
        function 
    getRows () {
            
            return 
    $this->numrows;
                
        }
        
        function 
    getLastId () {
            
            return 
    mysql_insert_id();
                
        }
        
        

    Ok, now in theory I am still using OOP, this SHOULD work since it uses the same connection structure as your's right? I see it does, private and public. So the $db should be private and publicized.

    This still doesn't work:

    PHP Code:
    $db1 = new sql($dbhost$dbuser$dbpass$dbname); 
    $db2 = new sql($dbs[5], $dbs[6], $dbs[7], $dbs[4]); 
    $dbs $db1->rowSelect("SELECT * FROM table WHERE id = {$_POST[id]}"); 
    In theory, when I private the code it should work right??

    Let's say DB1 is "code" and DB2 is "tables". This is the error I get still:

    Code:
    Error in SQL: Table 'tables.table' doesn't exist - Query: SELECT id, name FROM table
    And it should be "code.table". So as you can see DB1 is now using DB2, which doesn't make any sense at all since it's a new Object?

    Leaving the coding styles aside and the fact that we both do things differently, this should work now.
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #10
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Ok, now in theory I am still using OOP, this SHOULD work since it uses the same connection structure as your's right? I see it does, private and public. So the $db should be private and publicized.
    Sorry I meant to say "private and NOT publicized" ha ha
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #11
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Sorry posting the wrong SQL code, it's:

    PHP Code:
    $db1 = new sql($dbhost$dbuser$dbpass$dbname);
    $dbs $db1->rowSelect("SELECT * FROM tables WHERE id = {$_POST[id]}");
    $db2 = new sql($dbs[5], $dbs[6], $dbs[7], $dbs[4]);
    $chur $db1->rowSelectRet("SELECT id, name FROM table"); 
    Error:

    Error in SQL: Table 'tables.table' doesn't exist - Query: SELECT id, name FROM table
    The error happens in the second query string, the $chur, the first one works fine of course, then it opens the new object.
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You need to choose what you want to do. It doesn't make sense that static and non-static should ever be mixed. Forcing static on members guarantees that you cannot establish multiple connections since they will all share the same member properties, and even using if/else checks for is_object (which also doesn't make sense since this is always an object, regardless of if __construct has been called) doesn't change this.
    Note as well that T::$t is not the same as $t->t. Lets see if we can write something to show this.
    PHP Code:
    <?php

    class T
    {
        static 
    $t 'default';
        public static function 
    getST()
        {
            return 
    self::$t;
        }
        public function 
    getT()
        {
            return 
    $this->t;
        }
    }

    $a = new T();
    $b = new T();
    $a->'mkay';

    T::$t 'changed from default.';
    printf('$a::$t=%s, $a->t=%s' PHP_EOL$a->getST(), $a->getT());
    printf('$b::$t=%s, $b->t=%s' PHP_EOL$b->getST(), $b->getT());
    ?>
    Run that. You'll see that ::$t <> ->t in both cases. Also in both cases you'll see that ::$t is equivalent between both $a and $b. I believe this is actually a bug in PHP, or at minimum an attempt to retain BC; you cannot redeclare a member property regardless of if one is static or dynamic, but you can create a dynamic member on the fly which corresponds to an identically named static property. Just add that to the stack of really confusing 'features' in PHP.

    Edit:
    Oh, there's been about 4 more posts since I started writing this. Oh well.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You are overcomplicating this class completely. Its simply an intermediary, you don't need to put this much work into it.
    The only member you actually need is the connection resource, and POSSIBLY the last query if you want to keep it for whatever reason. You don't need to store anything else; the return doesn't appear to ever be used, the result is irrelevant since this isn't a Traversable type, and the numrows is also a little redundant. I also see globals in here which is just a bad idea. Either pass in or return variables that can be used instead, or configure anything necessary with additional member properties. I strongly recommend that you do not print from your methods. There is no rule that says you'll be using this in an HTML environment; return the results instead, or populate a provided variable with the results. Your goal should be to eliminate as many dependencies as you can between this class, other classes and configuration variables via global.

  • #14
    Regular Coder jfreak53's Avatar
    Join Date
    May 2004
    Location
    Guatemala
    Posts
    477
    Thanks
    19
    Thanked 10 Times in 10 Posts
    Quote Originally Posted by Fou-Lu View Post
    You'll see that ::$t <> ->t in both cases. Also in both cases you'll see that ::$t is equivalent between both $a and $b. I believe this is actually a bug in PHP, or at minimum an attempt to retain BC; you cannot redeclare a member property regardless of if one is static or dynamic, but you can create a dynamic member on the fly which corresponds to an identically named static property. Just add that to the stack of really confusing 'features' in PHP.
    So, does that mean I should create a NEW variable each time a new Object is called? Does $db get overwritten even though it's a new object and private?

    That is what I'm getting at, is $db being overwritten?? Shouldn't it NOT be overwritten?

    Sorry Fou, that confused me ha ha ha
    "FORTRAN is not a language. It's a way of turning a multi-million dollar mainframe, into a $50 programmable scientific calculator."
    http://www.microfastcat.com -- FastCat Software, the fastest software on the NET!
    http://www.microthosting.com -- Free reseller web hosting, Hosting, VPS, FREE SMALL HOSTING!!!
    http://www.microtronix-tech.com -- Web design and programming

  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    $db is a dynamic property. Its never overwritten since it belongs to the memory scope provided by the new.
    PHP Code:
    $a = new T();
    $b = new T();
    $a->'a\'s t';
    $b->'b\'s t'
    Neither can see each other unless you write a comparator so you can scope both. But then $this will never refer to the other, it will only refer to the calling class' $t.

    Using a static $t will always result in a class level overwrite. That is what static is, it means that its scoped to the class, not an instance of that class. Useful in singletons.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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