我有这个在 PHP 5.1 左右编写的旧代码,我试图让它在运行 php5.4 的较新服务器上运行,但是我从下面的代码中得到这个错误:
致命错误:在非对象上调用成员函数 fetch()
<?php
/**
* <b>Database Connection</b> class.
* @author Php Object Generator
* @version 3.0 / PHP5.1
* @see http://www.phpobjectgenerator.com/
* @copyright Free for personal & commercial use. (Offered under the BSD license)
*/
Class Database
{
private function Database()
{
$databaseName = $GLOBALS['configuration']['db'];
$driver = $GLOBALS['configuration']['pdoDriver'];
$serverName = $GLOBALS['configuration']['host'];
$databaseUser = $GLOBALS['configuration']['user'];
$databasePassword = $GLOBALS['configuration']['pass'];
$databasePort = $GLOBALS['configuration']['port'];
$databaseName = 'bitbsoft_butlers';
$driver = $GLOBALS['configuration']['pdoDriver'];
$serverName = 'localhost';
$databaseUser = 'bitbsoft_butlers';
$databasePassword = 'Sherw00d123';
$databasePort = '3306';
if (!isset($this->connection))
{
$this->connection = new PDO($driver.':host='.$serverName.';port='.$databasePort.';dbname='.$databaseName, $databaseUser, $databasePassword);
$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
if (!$this->connection)
{
throw new Exception('I cannot connect to the database. Please edit configuration.php with your database configuration.');
}
}
public static function Connect()
{
static $database = null;
if (!isset($database))
{
$database = new Database();
}
return $database->connection;
}
public static function Reader($query, $connection)
{
try
{
$result = $connection->Query($query);
}
catch(PDOException $e)
{
return false;
}
return $result;
}
public static function Read($result)
{
try
{
return $result->fetch();
}
catch (PDOException $e)
{
return false;
}
}
public static function NonQuery($query, $connection)
{
try
{
$r = $connection->query($query);
if ($r === false)
{
return 0;
}
return $r->rowCount();
}
catch (PDOException $e)
{
return false;
}
}
public static function Query($query, $connection)
{
try
{
$i = 0;
$r = $connection->query($query);
foreach ($r as $row)
{
$i++;
}
return $i;
}
catch (PDOException $e)
{
return false;
}
}
public static function InsertOrUpdate($query, $connection)
{
try
{
$r = $connection->query($query);
return $connection->lastInsertId();
}
catch (PDOException $e)
{
return false;
}
}
}
?>
现在 fetch() 行抛出了错误,但我很困惑从 5.1 到 5.4 的变化会导致这种情况,非常感谢任何帮助。
非常感谢