www.webdeveloper.com
Results 1 to 4 of 4

Thread: Help creating a MySQL connection class in OOP?

  1. #1
    Join Date
    Mar 2005
    Posts
    172

    Help creating a MySQL connection class in OOP?

    Hello,

    I'm trying to create a class named MySQL database as kindly suggested by NogDog in an earlier thread that will connect to a database and also query a table.

    However, i'm getting an error message mysql_fetch_array() supplied argument invalid.

    I think the database is connecting OK, but it's just not selecting the data?

    Would anyone be able to help me create this class so it pulls out the data, and also maybe help me identify any problems that may occur using my code, as i'm new to OOP as maybe I can do certain things better here?

    PHP Code:
    <?php

    //* Set up database contstants
    define("DB_SERVER","localhost");
    define("DB_USER","***********");
    define("DB_PASSWORD","*****");
    define("DB_NAME","************");

    class 
    MySQLDatabase {

      private 
    $connection;

    //* Open Database Connection and connect
      
    function __construct() {
        
    $this->connection $connection;
        
    $this->connection mysql_connect(DB_SERVERDB_USERDB_PASSWORD) or die ('Unable to select database: '.mysql_error ());
        
    $db_select mysql_select_db(DB_NAME,$this->connection);
      }  
      
    //* Query the Database
        
    function queryDatabase($sql) {
          
    $result mysql_query($sql$this->connection) or die ('Database query failed: '.mysql_error ());
        }

    }

    // * Create an instance of database
    $database = new MySQLDatabase();

    $sql "SELECT * from users WHERE id = 1";
    $result $database->queryDatabase($sql);
    $row mysql_fetch_array($result);
    echo 
    $row['username'];

    ?>
    Thanks in advance

    Chris
    Last edited by chrisb; 07-22-2010 at 08:52 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,332
    You need to return the result in the query method.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2005
    Posts
    172
    Thanks Nog Dog knew it was something simple!

    Also quick question if I may, while I was putting it together from what i've learnt online and other resources I was was wondering why I have to declare $connection as private and state that $this->connection = $connection as the code seems to work without both parts of my code, I don't see it's use in this case, surely I could just have have my __construct method as?

    PHP Code:
    class MySQLDatabase {

    //* Open Database Connection and connect
      
    function __construct() {
        
    $this->connection mysql_connect(DB_SERVERDB_USERDB_PASSWORD) or die ('Unable to select database: '.mysql_error ());
        
    $db_select mysql_select_db(DB_NAME,$this->connection);
      }  


    rather than
    PHP Code:
    class MySQLDatabase {

      private 
    $connection//WHY NEEDED?

    //* Open Database Connection and connect
      
    function __construct() {
        
    $this->connection $connection//WHY NEEDED?
        
    $this->connection mysql_connect(DB_SERVERDB_USERDB_PASSWORD) or die ('Unable to select database: '.mysql_error ());
        
    $db_select mysql_select_db(DB_NAME,$this->connection);
    }


    without those //WHY NEEDED bits, can anyone explain as to why they are needed please as i'd like to understand this?
    Last edited by chrisb; 07-22-2010 at 11:08 AM.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,332
    The class variable $connection is there so you can use it in any method that needs to specify the mysql connection, such as you do in the query() method when you call mysql_query(). As far as the $this->connectio=$connection in the constructor, it currently makes no since as you are not passing a connection to the constructor, but instead have it creating its own connection on the next line.

    On a side note, I would prefer not to make the class dependent on those constants being defined for the connection parameters, but instead explicitly provide them as constructor parameters.

    PHP Code:
    <?php

    class MySQLDatabase
    {
       private 
    $connection;
       public function 
    __construct($host$user$pwd$databse)
       {
          
    $connx mysql_connect($host$user$pwd);
          if(
    $connx == false) {
             throw new 
    Exception(mysql_error());
          }
          
    $this->connection $connx;
          
    $db mysql_select_db($database);
          if(
    $db == false) {
             throw new 
    Exception(mysql_error());
          }
       }
       public function 
    query($sql)
       {
          
    $result mysql_query($sql$this->connection);
          if(
    $result == false) {
             
    user_error(mysql_error() . "<br />\n$sql");
          }
          return 
    $result;
       }
    }
    Last edited by NogDog; 07-22-2010 at 12:11 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles