我有这个脚本使用php pdo访问mysql数据库,但问题是我的托管服务提供商已禁用php pdo所以现在我卡住了,不知道如何更改此代码以访问没有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 db
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;
}
//retrive 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 />";
?>
我仍然是PHP的新手,这对我来说太过激进,希望有人可以提供帮助。 谢谢!
答案 0 :(得分:2)
如果您的主机已禁用PDO,我强烈建议您切换主机!
如果无法做到,可以替换mysqli *函数的PDO函数。
示例:
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);
}
...
}
答案 1 :(得分: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);
冲洗,起泡,重复其余的课程方法