gpt4 book ai didi

php - 如何在不使用 PHP PDO 的情况下访问 MySQL 数据库?

转载 作者:行者123 更新时间:2023-11-29 08:44:52 26 4
gpt4 key购买 nike

我有一个使用 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 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/>";
?>

最佳答案

如果您的托管服务商禁用了 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);
}

...
}

关于php - 如何在不使用 PHP PDO 的情况下访问 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12757954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com