Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Class Import

Tags:

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?

like image 833
Helen Neely Avatar asked Oct 28 '11 13:10

Helen Neely


1 Answers

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

like image 181
Roman Avatar answered Oct 08 '22 03:10

Roman