object-oriented question

Posted by user522962 on Stack Overflow See other posts from Stack Overflow or by user522962
Published on 2010-12-27T23:39:07Z Indexed on 2010/12/27 23:53 UTC
Read the original article Hit count: 132

Filed under:

I am attempting to put all my database connections in 1 php file, rather than in each of my individual php pages. I have the following:

//conn.php:

<?php

class conn {
   var $username = "name";
   var $password = "password";
   var $server = "localhost";
   var $port = "3306";
   var $databasename = "db";
   var $tablename = "tablename";
   var $connection;

   public function getConnected() {

       $this->connection = mysqli_connect(
       $this->server,
       $this->username,
       $this->password,
       $this->databasename,
       $this->port
        );
}


}
?>

// file.php:

<?php
require_once("conn.php");

class myClass{

public function con() {

$conn = new conn();
$conn->getConnected();    
}

public function myF() {
$stmt = mysqli_prepare($conn->connection, "SELECT * FROM $conn->tablename");

mysqli_stmt_execute($stmt);
    }
}
?>

I then call this as follows:

$myNew = new myClass(); $myNew->con(); $myNew->myF();

When I call this, I get the following error:

Undefined property: myClass::$connection

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about php