Retrieving data from MySQL using OOP

I am new to OOP PHP. I'm trying to create a class that will connect, query and retrieve data. I did the following encoding

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

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

and I want to get data using a format like this

echo $result[0];

but i don't get this logic to make this happen please help

Thank!

+3
source share
3 answers

Your function Fetchpulls only one row from the database and you are not returning results ...

The method is not the best, but to achieve what you are 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 cause each row to be part of the result of $ result, but you will have to access it as follows:

You call the fetch function as follows:

$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
}

, - ( , .)

: , ... , , , .

Edit2: , :

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!";
}

, ... , . , .

+10

, $result Fetch. Fetch.

$result= mysql_fetch_array($query); - return mysql_fetch_array($query);.

$row = $connect->Fetch('posts');
// do something with $row

Fetch , . mysql_query mysql_fetch_array , .

. PHP OOP, PDO, PHP. , , SQL-.

: http://www.giantflyingsaucer.com/blog/?p=2478

+1

, .

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

.

$post->returnDate("row name here");
0

All Articles