MYSQL database oops issue

Hi everyone,

I need a urgent help…i got some code from online…but i am not able to understand mysql which is already deprecated. I want to convert this code into mysqli with oops concepts.please help me…

here is code:

<?php

define("DBHOST","localhost");
define("DBUSER","root");
define("DBPWD","");
define("DB","prabbhas");

class DB_FUNCTIONS {

public function __construct() {
$conn = mysql_connect(DBHOST,DBUSER,DBPWD);
$db_select = mysql_select_db(DB,$conn);	
}

public function getResults($table) 
{
$data = array();
$query = mysql_query("SELECT * FROM $table") or die(mysql_error());
$num_rows = mysql_num_rows($query);
if($num_rows>0) {
while($row=mysql_fetch_assoc($query))
$data[]=$row;
}
return $data;	
}

public function allProducts()
{
$query = mysql_query("SELECT * FROM wallpapers");	
while($row=mysql_fetch_assoc($query))
$data[]=$row;

return $data;
}

public function getproductPhoto($id)
{
$photo = mysql_result(mysql_query("SELECT pic FROM wallpapers where id = $id"),0);	

return $photo;
}


}
?>

Is there a reason you’ve chosen to move to mysqli rather than PDO?

A good idea if you’re changing your code now. anyway. The timing is right.

For a rough idea as to what is involved migrating to mysqli_ procedural

Hi droopsnoot, i am interested in oops so plz if you can plz convert

This is what I do for PDO (It wouldn’t be hard to use this with mysqli):

<?php

namespace website_project\database;

use PDO;

class ConnectPDO {

    private $pdo_options = [
        /* important! use actual prepared statements (default: emulate prepared statements) */
        PDO::ATTR_EMULATE_PREPARES => false
        /* throw exceptions on errors (default: stay silent) */
        , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        /* fetch associative arrays (default: mixed arrays)    */
        , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
    ];
    
    public $connection = \NULL;

    public function __construct() {
        $this->connection = new PDO('mysql:host=' . \DATABASE_HOST . ';dbname=' . \DATABASE_NAME . ';charset=utf8', \DATABASE_USERNAME, \DATABASE_PASSWORD, $this->pdo_options);
    }
    
    public function getDatabase() {
        return $this->connection;
    }

}

then in my configuration file I have this:

$db = new Connect;
$pdo = $db->getDatabase();

Then if I want another class to use the database connection I simply do this:

<?php
namespace website_project\blog;
/* Location of the ConnectPDO Class ($PDO Connection String) */
use website_project\database\ConnectPDO as ConnectPDO;
use PDO;
class Blog implements iCRUD {

    private $connectPDO;
    private $pdo;
    protected $query;
    protected $stmt;
    protected $sql;
    protected $result;
    protected $queryParams;
    protected $author;
    public $username;
    protected $row;

    public function __construct(ConnectPDO $connectPDO) {
        $this->connectPDO = $connectPDO;
        $this->pdo = $this->connectPDO->getDatabase();
    }

    public function create($data) {

        $this->query = "INSERT INTO pages (creator_id, title, content, date_added) VALUES (:creator_id, :title, :content, NOW()) ";
        try {

to utlilize this class properly when you create an instance you have to do the following:

$blog = new Blog($db); // $db is the connectin string class:

I used namespaces but that can be rectified by taking that out, but I still recommend using an autoloader. :wink:

HTH John

I’m happy to offer suggestions to code changes that you make and post as far as I am able, but sorry, I’m not going to just do the conversion for you. Have a read up on mysqli and PDO from the links provided and give it a go.

Thanks i have solved this issue.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.