Skip to content
Advertisement

How can I access a MySQL database without using PHP PDO?

I have this script which uses PHP PDO to access the MySQL database, but the problem is my hosting provider has disabled PHP PDO, so now I’m stuck and have no idea how to change this code to access the database without PHP PDO:

<?php

    class DbHandler {

        private $dbname = '**********';
        private $host = '**********';
        private $user = '**********';
        private $pass = '**********';
        public $dbh;
        private $sth;

        public function __construct()
        {
            try{
                $this->dbh = new PDO("mysql:$this->host=localhost;dbname=$this->dbname", $this->user, $this->pass);
            }
            catch(PDOException $e){
                echo 'Unable to connect to database!';
            }
        }

        // Retrive all replays from the database
        public function selectAll($offset, $rowsperpage){

            $this->sth = $this->dbh->query("SELECT game_id, game_title,game_date_upload,game_file_name FROM games ORDER BY game_id DESC LIMIT $offset, $rowsperpage");
            $this->sth->setFetchMode(PDO::FETCH_ASSOC);
            $replays = $this->sth->fetchAll();

            return $replays;
        }

        // Return number of replays from db
        public function numOfReplays(){
            $this->sth = $this->dbh->query("SELECT game_id FROM games");
            $this->sth->setFetchMode(PDO::FETCH_ASSOC);
            $replays = $this->sth->fetchAll();
            $numOfReplays = count($replays);
            return $numOfReplays;
        }

        // Search db
        public function search1($search_text, $offset, $rowsperpage){
            $this->sth = $this->dbh->query("SELECT game_id, game_title,game_date_upload,game_file_name FROM games WHERE game_title LIKE '%%$search_text%%' ORDER BY game_id DESC LIMIT $offset, $rowsperpage");
            $this->sth->setFetchMode(PDO::FETCH_ASSOC);
            $replays = $this->sth->fetchAll();
            return $replays;
        }

        public function search($search_text, $offset, $rowsperpage){
            $search_text = '%%' . $search_text . '%%';
            $this->sth=$this->dbh->prepare("SELECT game_id, game_title,game_date_upload,game_file_name FROM games WHERE game_title LIKE ? ORDER BY game_id DESC LIMIT $offset, $rowsperpage");
            $this->sth->setFetchMode(PDO::FETCH_ASSOC);
            $this->sth->execute(array($search_text));
            $replays = $this->sth->fetchAll();
            return $replays;
        }

        public function numOfSearchResults($search_text){
            $search_text = '%%' . $search_text . '%%';
            $this->sth = $this->dbh->prepare("SELECT game_id FROM games WHERE game_title LIKE ?");
            $this->sth->setFetchMode(PDO::FETCH_ASSOC);
            $this->sth->execute(array($search_text));
            $replays = $this->sth->fetchAll();
            $numOfReplays = count($replays);
            return $numOfReplays;
        }

        // Retrieve last five uploaded replays
        public function latestReplays(){
            $this->sth = $this->dbh->query("SELECT game_title,game_file_name FROM games ORDER BY game_date_upload DESC LIMIT 2");
            $this->sth->setFetchMode(PDO::FETCH_ASSOC);
            $lastReplays = $this->sth->fetchAll();

            return $lastReplays;
        }

        // Insert replay data in db
        public function exec($data=array())
        {
            $this->sth = $this->dbh->prepare("INSERT INTO games(game_title,game_description,game_file_name) values(?,?,?)");
            $this->sth->execute($data);
        }
    }
?>

My database works fine as I have used this MySQL test script to connect to it and it works:

<?php
    mysql_connect("myhost.com", "username", "password") or die(mysql_error());
    echo "Connected to MySQL<br/>";
?>

Advertisement

Answer

As JvdBerg says, you can switch hosts or change to MySQLi

Here is a quick change from PDO to MySQLi

From

$this->dbh = new PDO("mysql:$this->host=localhost;dbname=$this->dbname", $this->user, $this->pass);`

to

$this->dbh = new mysqli($this->host,$this->user,$this->pass,$this->dbname);

And

from

$this->sth = $this->dbh->query("SELECT game_id, game_title,game_date_upload,game_file_name FROM games ORDER BY game_id DESC LIMIT $offset, $rowsperpage");
$this->sth->setFetchMode(PDO::FETCH_ASSOC);
$replays = $this->sth->fetchAll();

to

$this->sth = $this->dbh->query("SELECT game_id, game_title,game_date_upload,game_file_name FROM games ORDER BY game_id DESC LIMIT $offset, $rowsperpage");
$replays = $this->sth->fetch_all(MYSQLI_ASSOC);

Rinse, lather, repeat for the rest of the class methods.

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