SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Beggining OOP - MySQL Class Probs

    Hiya,

    Ive just started trying to play around with OOP a little bit and wondered if someone could help me with this. Here are the 3 bits of code I am using. I dont get any errors but nothing is inserted into the database. I am fairly sure its something simple, just dont know what.

    Stats.class.php

    PHP Code:

    include("dao.class.php");

    class 
    statTracker {

        var 
    $mysql;
        var 
    $conn;

        function 
    runTracker($ip$browser) {
        
            
    $this->mysql = &new MySQL();
            
    $this->conn $this->mysql->connectToDb();
            
    $sql =  "INSERT INTO
                        stat
                     SET
                        ip = '
    $ip',
                        browser = '
    $browser',
                        date = NOW()"
    ;
            
    $this->mysql->query($sql);
        }

    dao.class.php

    PHP Code:

    class MySQL {

        var 
    $dbHost;
        var 
    $dbUser;
        var 
    $dbPass;
        var 
    $dbName;
        var 
    $dbConn;
        var 
    $dbSel;
        var 
    $dbConnError;
        var 
    $dbSelError;
        
        function 
    MySQL(){
        
            
    $this->dbHost 'localhost';
            
    $this->dbUser '';
            
    $this->dbPass '';
            
    $this->dbName 'mark';

        }
        
        function 
    connectToDb(){
        
            
    $this->dbConn mysql_connect($this->dbHost$this->dbUser$this->dbPass);
            if(!
    $this->dbConn) {
                
    $this->dbConnError "Error connecting to database";
                return 
    $this->dbConnError;
            }
            if(
    $this->dbConn) {
                
    $this->dbSel mysql_select_db($this->dbName);
            }
            if(!
    $this->dbSel) {
                
    $this->dbSelError ="Error selecting the database";
                return 
    $this->dbSelError;
            }
        }

        function &
    query($sql) {
        
            return new 
    fetch($this, @$result);
            
        }
    }

    class 
    fetch {

        var 
    $connectToDb;
        var 
    $query;
        
        function 
    fetch($connectToDb$query) {
        
            
    $this->connection = &$connectToDb;
            
    $this->query $query;

        }

        function 
    fetchFromDb() {

            
    $row mysql_fetch_array($this->queryMYSQL_ASSOC);
            return 
    $row;

        }

    add.php

    PHP Code:

    include("stats.class.php");

    $track = &new statTracker();

    $ip $_SERVER['REMOTE_ADDR'];
    $browser $_SERVER['HTTP_USER_AGENT'];

    $track->runTracker($ip$browser); 

  2. #2
    SitePoint Enthusiast ssx-gun's Avatar
    Join Date
    Sep 2002
    Location
    Strongsville, OH
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suggest you insert some statements so it will tell you what its doing.

    E.g. wether or not it is connected to the database or not.

    Also try calling the mysql_error() function after you do your insert.
    PHP: Pills Help People
    ---
    weird-one.com


  3. #3
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I will try that... I dont think its a problem connecting to the DB though.

    Thanks

  4. #4
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, no probs connecting to or selecting the database! :S

  5. #5
    SitePoint Enthusiast ssx-gun's Avatar
    Join Date
    Sep 2002
    Location
    Strongsville, OH
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    did you try calling mysql_error() after the insert statement?
    PHP: Pills Help People
    ---
    weird-one.com


  6. #6
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep....and everythings fine

  7. #7
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm..... when i cange the line...

    PHP Code:

    $this
    ->mysql->query($sql); 
    to

    PHP Code:

    $result 
    mysql_query($sql); 
    it works....

  8. #8
    If it aint Dutch it aint much Kilroy's Avatar
    Join Date
    Oct 2003
    Location
    The Netherlands
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $this->mysql->query($sql); 
    should be
    PHP Code:
    $result $this->mysql->query($sql); 

  9. #9
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Kilroy!!

    Ok, Ive changed it round a little now and have things working how I want.

    stats.class.php

    PHP Code:

    include("dao.class.php");

    class 
    statTracker {

        var 
    $mysql;
        var 
    $conn;
        var 
    $query;
        var 
    $result;
        

        function 
    runTracker($ip$browser) {
        
            
    $this->mysql = &new MySQL();
            
    $this->conn $this->mysql->connectToDb();
            
    $this->query "INSERT INTO
                            stats
                             SET
                            ip = '
    $ip',
                            browser = '
    $browser',
                            date = NOW()"
    ;
            
    $this->mysql->dbQuery($this->query);
        }
        
        function 
    viewTracker() {
        
            
    $this->mysql = &new MySQL();
            
    $this->conn $this->mysql->connectToDb();
            
    $this->query "SELECT * FROM
                            stats"
    ;
            
    $this->result $this->mysql->dbQuery($this->query);
        }

    dao.class.php

    PHP Code:

    class MySQL {

        var 
    $dbHost;
        var 
    $dbUser;
        var 
    $dbPass;
        var 
    $dbName;
        var 
    $dbConn;
        var 
    $dbSel;
        var 
    $dbQuery;

        function 
    MySQL(){

            
    $this->dbHost 'localhost';
            
    $this->dbUser '';
            
    $this->dbPass '';
            
    $this->dbName 'mark';

        }
        
        function 
    connectToDb() {
        
            
    $this->dbConn mysql_connect($this->dbHost$this->dbUser$this->dbPass)
            or die (
    mysql_error($this->dbConn));
            if(isset(
    $this->dbConn)) {
                
    $this->dbSel mysql_select_db($this->dbName)
                or die (
    mysql_error($this->dbSel));
                if(isset(
    $this->dbSel)) {
                    return 
    TRUE;
                } else {
                    return 
    FALSE;
                }
            }
        }
        
        function 
    dbQuery($query) {
        
            
    $this->dbQuery $query;
            return 
    mysql_query($query$this->dbConn);
            
        }
        
        function 
    dbFetch($result) {
        
            return 
    mysql_fetch_array($result);
            
        }
        

    add.php

    PHP Code:

    include("stats.class.php");

    $track = &new statTracker();

    $page $_GET['page'];

    if(
    $page == 'runTracker') {

        
    $ip $_SERVER['REMOTE_ADDR'];
        
    $browser $_SERVER['HTTP_USER_AGENT'];

        
    $track->runTracker($ip$browser);

    }

    if(
    $page == 'viewTracker') {

        
    $track->viewTracker();
        
        while(
    $row $track->mysql->dbFetch($track->result)) {
            
    $ip $row['ip'];
            
    $browser $row['browser'];
            echo 
    'IP:' $ip '<br />';
            echo 
    'BROWSER:' $browser '<br />';
        }



Bookmarks

Posting Permissions

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