dongzhuo3202 2011-10-28 13:10
浏览 8
已采纳

PHP类导入

Here's a sample code that I'm trying to call from another page to help me with stats. I can't seem to get it to work. How do I import and call this class in another php file? stats.php

<?php

include("config.php");
$link = mysql_connect($host, $username, $password);
mysql_select_db("mydb", $link);

class stats{

  function newReg(){

    $result = mysql_query("SELECT * FROM people where status ='registered' ", $link);
    $num_rows = mysql_num_rows($result);
     return $num_rows ;


function newApp(){
    $result = mysql_query("SELECT * FROM people where status = 'NEW' ", $link);
    $num_rows = mysql_num_rows($result);

    return $num_rows;
 }
?>

I want to call that class here in another file:

<?php

require_once("stats.php");
   echo(stats.newReg());

 ?>

Is there something I'm missing here?

  • 写回答

2条回答 默认 最新

  • dqzd92796 2011-10-28 13:12
    关注

    you forgot 2 closing brackets

    <?php
    
    include("config.php");
    $link = mysql_connect($host, $username, $password);
    mysql_select_db("mydb", $link);
    
    class stats{
    
      function newReg(){
        global $link;
        $result = mysql_query("SELECT * FROM people where status ='registered' ", $link);
        $num_rows = mysql_num_rows($result);
         return $num_rows ;
      }
    
    function newApp(){
        global $link;        
        $result = mysql_query("SELECT * FROM people where status = 'NEW' ", $link);
        $num_rows = mysql_num_rows($result);
    
        return $num_rows;
     }
    }
    ?>
    

    anyway, other file:

    include 'statsclassfile.php';
    $myStats = new stats();
    $mystats->newReg();
    

    PS: naming conventions generally want that class names begin with a capital letter, eg: Stats

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码