【发布时间】:2015-12-29 19:33:10
【问题描述】:
我们将 php 从 5.3 升级到 5.6,现在我收到错误和警告。我对导致这种情况的 php 5.3 弃用的内容有点困惑。
PHP Warning: get_class() called without object from outside a class in /home/website/public_html/php/search.class.php on line 6
PHP Warning: Creating default object from empty value in /home/website/public_html/favorites/favorite.class.php on line 25
PHP Fatal error: Call to a member function getEntry() on a non-object in /home/website/public_html/php/search.class.php on line 43
get_class 警告
if( get_class($favorite) === false )
$favorite = new favorite;
$commQuery 是第 43 行致命错误
class getCommDetails{
var $community;
var $commURL;
var $commAddress;
var $commPhone;
var $cid;
var $commMin;
var $commMax;
function getCommDetails($community){
$ret = false;
if( get_class($db) === false )
$db = new DB(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
$this->community = $community;
$commQuery = $db->getEntry('communities', '*', "communities.community = '$this->community'");
if( $db->numrows($commQuery) === 1) {
$commResults = $db->fetch_array($commQuery);
$this->commURL = $commResults['url'];
$this->commAddress = $commResults['address'];
$this->commPhone = $commResults['sales_phone'];
$this->cid = $commResults['cid'];
$this->vars = $commResults;
//$this->commURL = $fpResults
$ret = true;
}
return $ret;
}
};
数据库连接代码
<?php
class DB {
// Class Variables
var $connection;
var $queryStr;
// Class Constructor
function DB($DB_Server, $DB_User, $DB_Pass, $DB_Name){
$this->connection = mysqli_connect("$DB_Server", "$DB_User", "$DB_Pass","$DB_Name") or die("MySQL Connection Err: ". mysqli_error($this->connection));
//mysql_select_db("$DB_Name") or die("MySQL DB Err: ".mysql_error());
}
// Class Functions
function addEntry($table, $fieldlist, $valuelist){
$this->queryStr = "INSERT INTO `". $table ."` ($fieldlist) VALUES ($valuelist)";
return $this->query($this->queryStr);
}
function updateEntry($table, $fieldvalues, $where = NULL){
if( is_array($fieldvalues) ){
$c=0;$numFields=count($fieldvalues);
foreach($fieldvalues as $fieldname => $value){
if($fieldname == 'inventoryAction'){
$c++;
continue;
}
$fieldvaluelist .= $fieldname."='".$value."'";
if($numFields > 1){
if($c < $numFields-1)
$fieldvaluelist .= ",";
}
$c++;
}
} else {
return false;
}
$this->queryStr = "UPDATE `". $table ."` SET ".$fieldvaluelist;
if($where !== NULL)
$this->queryStr .= " WHERE $where";
return $this->query($this->queryStr);
}
function removeEntry($table, $where = NULL){
if($where != NULL){
$this->queryStr = "DELETE FROM `". $table ."` WHERE $where";
return $this->query($this->queryStr);
} else {
return false;
}
}
function getEntry($table, $fieldlist, $where=NULL, $whereon=NULL, $groupby = NULL){
$this->queryStr = "SELECT $fieldlist FROM ".$table;
if($whereon !== NULL)
$this->queryStr .= " ON $whereon";
if($where !== NULL)
$this->queryStr .= " WHERE $where";
if($groupby !== NULL)
$this->queryStr .= " GROUP BY $groupby";
return $this->query($this->queryStr);
}
function fetch_array($qResults, $recordType = NULL){
if($recordType == "MYSQL_NUM"){
return mysqli_fetch_array($qResults, MYSQL_NUM);
} elseif($recordType == "MYSQL_ASSOC"){
return mysqli_fetch_array($qResults, MYSQL_ASSOC);
} else {
return mysqli_fetch_array($qResults);
}
}
function fetch_object($qResults){
return mysqli_fetch_object($qResults);
}
function numrows($qResults){
if(!$qResults)
return false;
else
return mysqli_num_rows($qResults);
}
function getLastID(){
return mysqli_insert_id($this->connection);
}
function getQuery(){
return $this->queryStr;
}
function query($query){
if(!$query)
return false;
else
return mysqli_query($this->connection,$query);
}
};
?>
【问题讨论】:
-
我格式化了你的警告,修正了你的一些大写,让你的大胆保持一致。不过,您应该尝试将一些代码缩减到相关点。
-
回答为什么您会看到其中一些错误 - 在您使用
get_class()时,$db未在函数范围内定义。在您的旧设置中,您可能没有最多 E_ALL 的 error_reporting,因为这会生成E_NOTICE undefined variable $db。 “来自空值的默认对象”现在发生了,因为如果您在 E_ALL 处确实有 error_reporting,则在 5.4 之前它不包括 E_STRICT 警告,但后来有。这是严重的违规行为。 stackoverflow.com/questions/8900701/…
标签: mysql upgrade php-5.3 php-5.6