【问题标题】:Joomla website breaks when include custom code fileJoomla 网站在包含自定义代码文件时中断
【发布时间】:2012-12-27 03:07:39
【问题描述】:

我在我的网站上发布了一个问题 (http://stackoverflow.com/questions/14263141/joomla-website-not-working-after-moving-to-new-server)。并注意到问题只有在我在网站上插入一些我拥有和需要的代码时才会发生。在代码中我有一个类并在我的文件周围使用它。

工作了大约 2 个月,现在不起作用。我没有更改代码上的任何内容,并且过去在以前的模板中使用过它。

我想我没有按照 Joomla 喜欢的方式正确地包含它,但是无法理解发生了什么。

这是一个简单的数据库连接和链接 ID 的类:

// Class db: Connects to database and returns linkid.
class MY_DB{

  var $sqlhost; 
  var $sqluser;
  var $sqlpass;
  var $sqldb;
  var $err;
  var $status;
  var $num_rows;
  var $result;
  var $linkid;
  var $query;
  var $rows     = Array();
  var $last_insert_id;

  function MY_DB( $query="", $db="" ){

    if( $db != "" ){ $this->sqldb = $db; }
    else{ $this->sqldb = "dbase"; }

    $this->sqlhost  = "localhost";
    $this->sqluser  = "root";
    $this->sqlpass  = "pass";


    $this->query    = $query;

    if( $this->query == "" ){
      $this->__Connect__();
    }
    else{
      $this->__Connect__();
      $this->__TalkToDB__();
    }




  }// end constructor Session


///////////////////////////////////////// 
  function __Connect__(){
      //connect to mysql
      $this->linkid = mysql_connect( $this->sqlhost, $this->sqluser, $this->sqlpass );

      if( $this->linkid ){
        $this->result = mysql_select_db( $this->sqldb, $this->linkid );
      }
      else
        $this->err = "Could not connect to MySQL server";
    }// end Connect function
/////////////////////////////////////////    
  function __TalkToDb__(){

      $this->result = mysql_query( $this->query, $this->linkid );

      if( !$this->result ){
        echo ( "Query: '" . $this->query . "', failed with error message: -- " . mysql_error() . " --" );
      }     
  }// end TalkToDb function
//////////////////////////////////////////

  function __CountRows__(){
      $this->num_rows = mysql_num_rows( $this->result );

      return $this->num_rows;

  }

//////////////////////////////////////////  
  function __LastInsertedId__()
  {
    return mysql_insert_id( $this->linkid );
  }


}// end class definition

?>

这是我包含的另一个文件:

<?php
// select random activity
$num_acts_display = 2;

$query = "select id from activity where enabled='Y'";
$all_ids  = array();

$act_id   = new MY_DB( $query ); 


while( $all = mysql_fetch_array($act_id->result,MYSQL_BOTH) ){

    $all_ids[] = $all['id'];

}

// shuffle it
shuffle( $all_ids );
// re-shuffle it
shuffle( $all_ids );

?>
<div class="random-figureo">
    <ul>  
<?

for( $i = 0; $i < $num_acts_display; $i++ ){

  $query = "select * from activity where id=".$all_ids[$i];
  $act = new MY_DB( $query ); 
  $a = mysql_fetch_array($act->result,MYSQL_BOTH)


?>

  <li>
    <div class="random_img">
     <a href="<?=$this->baseurl?>/index.php?option=com_content&view=article&id=537&Itemid=194&page=thumbnails&amp;act_id=<?=$a['id']?>">
     <?
      $portada = get_portada($a['id'], "", true);

      if( $portada == "none" ){
     ?>
      <div style="background:#666; width:100px; height:65px; padding:0px; margin:0px;"></div>
     <?}
        else{// "templates/" . $this->template .
     ?> 
       <img src="<?= "templates/" . $this->template . substr( $a['directory'],1,strlen($a['directory']) ) . "/" . $portada ?>" width="100" height="65" alt="<?=$a['act_name']?>" />
     <?}?>
     </a>
    </div>
    <br />
    <div class="random_title">
      <a href="<?=$this->baseurl?>/index.php?option=com_content&view=article&id=537&Itemid=194&page=thumbnails&amp;act_id=<?=$a['id']?>"><?=$a['act_name']?></a>
    </div>
  </li>   
<?
}
?>      
  </ul>
</div>

我是这样包含它的:

include( "templates/" . $this->template . "/includes/db.class" );

include( "templates/" . $this->template . "/random-figureo.php" ); 

我做错了什么?我正在使用 J!2.5.8 和 PHP 版本:5.3

【问题讨论】:

  • 当它不起作用时您收到的错误信息是什么?
  • 既然你说你搬到了新服务器,我会仔细检查以确保你的 MySQL 主机名$this-&gt;sqlhost = "localhost"; 确实是localhost。我记得我的网站崩溃了,因为主机名已从 localhost 更改为域名,例如 db.myhost.com。
  • 如果我删除包含,一切都会完美。包含后,joomla 的模块不会加载,除了我包含的代码之外,大部分页面看起来都是空白的。
  • 如果我包含我的代码,只有我的代码显示而不是扩展。
  • 我不明白为什么你不能坚持 Joomla 编码标准而不是创建一个新的数据库类等等。会让生活变得更轻松,并可能解决您的问题。

标签: php joomla include joomla2.5


【解决方案1】:

我刚刚发现了问题:

Blockquote 该扩展自 PHP 5.5.0 起已弃用,并将在未来删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展。

所以,我将文件的 mysql 扩展名更改为 mysqli,现在一切正常...

我仍在使用 php 5.3,看起来它不喜欢 mysql_fetch_array...

感谢大家的宝贵时间。希望这对将来的其他人有所帮助。我花了三天的时间和时间感到沮丧,并且我的网站处于离线状态。

【讨论】:

  • 如果您已经解决了这个问题,请给您的答案打上绿色勾号,以将此页面标记为已解决,非常感谢。你在这个网站上有几个废弃的问题;请努力将每个问题都推进到系统认可的解决方案中。如果您有任何新的基于 Joomla 的问题,请加入 Joomla Stack Exchange 社区。​​span>
猜你喜欢
  • 2010-12-08
  • 2011-01-05
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 2012-03-26
  • 2018-01-01
  • 2023-04-02
相关资源
最近更新 更多