0

PHP PDO を使用して MySQL データベースにアクセスするこのスクリプトがありますが、問題は私のホスティング プロバイダーが 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);
        }
    }
?>

このMySQLテストスクリプトを使用して接続したため、データベースは正常に動作し、動作します。

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

2 に答える 2

2

ホスティング事業者がPDOを無効にしている場合は、ホスティング事業者を切り替えることを強くお勧めします。

それが不可能な場合は、PDO関数をmysqli*関数に置き換えることができます。

例:

class DbHandler
{

  private $dbname = '**********';
  private $host = '**********';
  private $user = '**********';
  private $pass = '**********';

  public  $dbh;
  private $sth;

  public function __construct()
  {
    $this->dbh = new mysqli( $this->host, $this->user, $this->pass, $this->dbname);
  }

  ...
} 
于 2012-10-06T08:07:14.977 に答える
0

JvdBerg が言うように、ホストを切り替えるか、MySQLiに変更することができます

PDO から MySQLi への簡単な変更は次のとおりです。

から

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

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

から

$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();

$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);

すすぎ、泡立て、残りのクラスメソッドについて繰り返します。

于 2012-10-06T08:14:46.503 に答える