Skip to content
Advertisement

Fetch data from MySQL using OOP

I’m a beginner in OOP PHP. I’m trying to make a class that will connect,query and fetch data I done the below coding

class MySQL {

    private $set_host;
    private $set_username;
    private $set_password;
    private $set_database;

     public function __Construct($set_host, $set_username, $set_password){
        $this->host = $set_host;
        $this->username = $set_username;
        $this->password = $set_password;
        $con= mysql_connect($this->host, $this->username, $this->password);
        if(!$con){ die("Couldn't connect"); }
    }


    public function Database($set_database)
    {   
        $this->database=$set_database;
        mysql_select_db($this->database)or die("cannot select Dataabase");
    }

    public function Fetch($set_table_name){
        $this->table_name=$set_table_name;
        $query=mysql_query("SELECT * FROM ".$this->table_name); 
    $result= mysql_fetch_array($query);
    }
}

$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$connect->Fetch('posts');

what I’m trying to achieve is this

$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$connect->Fetch('posts');

and I want to fetch the data using a format like this

echo $result[0];

but I’m not getting that logic to make this happen please help

Thanks!

Advertisement

Answer

Your Fetch function is only pulling one row from the database, and you aren’t returning the results…

The method isn’t the best, but in order to achieve what you’re trying to do:

public function Fetch($set_table_name){
    $query=mysql_query("SELECT * FROM ".$set_table_name); 
    $result = array();
    while ($record = mysql_fetch_array($query)) {
         $result[] = $record;
    }
    return $result;
}

This will make each row a part of $result, but you’ll have to access it like this:

You would call the fetch function like this:

$result = $connect->Fetch('posts');

echo $result[0]['columnName'];  // for row 0;

Or in a loop:

for ($x = 0; $x < count($result); $x++) {
   echo $result[$x][0] . "<BR>";  // outputs the first column from every row
}

That said, fetching the entire result set into memory is not a great idea (some would say it’s a very bad idea.)

Edit: It also looks like you have other issues with your class… I have to run but will check back tomorrow and if others haven’t set you straight I will expand.

Edit2: Ok, going to do a once-over on your class and try to explain a few things:

class MySQL {

  //declaring the private variables that you will access through $this->variable;
  private $host;  
  private $username;
  private $password;
  private $database;
  private $conn;  // Adding the connection, more on this later.

  public function __Construct($set_host, $set_username, $set_password){
    $this->host = $set_host;
    $this->username = $set_username;
    $this->password = $set_password;
    // Combining the connection & connection check using 'or'
    // Notice that I removed the semi-colon after the mysql_connect function
    $this->conn = mysql_connect($this->host, $this->username, $this->password)
                  or die("Couldn't connect");
  }

  public function Database($set_database)
  {   
    $this->database=$set_database;
    // Adding the connection to the function allows you to have multiple 
    // different connections at the same time.  Without it, it would use
    // the most recent connection.
    mysql_select_db($this->database, $this->conn) or die("cannot select Dataabase");
  }

  public function Fetch($table_name){
    // Adding the connection to the function and return the result object instead
    return mysql_query("SELECT * FROM ".$table_name, $this->conn);         
  }

}

$connect = new MySQL('localhost','root','');
$connect->Database('cms');
$posts = $connect->Fetch('posts');

if ($posts && mysql_num_rows($posts) > 0) {
     echo "Here is some post data:<BR>";
     while ($record = mysql_fetch_array($posts)) {
         echo $record[0];  // or a quoted string column name instead of numerical index.
     }
} else {
     echo "No posts!";
}

I hope this helps… It should get you started at least. If you have more questions you should ask them separately.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement