0

I need to write a method to return the number of rows I have in my table. This code I've written so far;

<pre><?php  
     class Database
{
	
	private $connected=false;
	private $user,$pass,$host;
	private $db;
	private $result=array();
	
	public function initialize($user, $pass, $host)
	{
	 $this->user=$user;
	 $this->pass=$pass;
	 $this->host=$host;
	 return $this;
		
    }
	
  function connect($dbname)
  
  {
   $this->db=mysql_connect($this->host,$this->user,$this->pass) or
   die('<h1>Unable to connect to database</h1>' . mysql_error()); 
   mysql_select_db($dbname) or die('<h1>Unable to select database</h1>' . mysql_error());
   return $this;
  }
  
  function query($sqlstring)
  {
	$query= mysql_query($sqlstring,$this->db) or die('<h1>Unable to query database</h1>' . mysql_error());  
	while($record=mysql_fetch_assoc($query)){
		$this->result[]=$record;
	}
	return $this->result;
  }

  /*function numRows($sq)
  {
    $this->$query="select count(*) as numRows from members";
  }*/

}

$db = new Database();
print_r($db->initialize("root","project","localhost")->connect("mmsplus")->
        query("SELECT count(*) as num FROM members"));

?></pre>

So far this does the work but I need a separate method for the counting, any help is appreciated.

4
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by aMOEBa
0

The option I would choose is a property last_count. Before the return in your query function you could do

$this->last_count = count($this->result);

Side note: At this point the query function does not reset your result property. Should you execute two queries after one another, result would contain both.

Edited by pritaeas: n/a

0

Use count function on result set. It will give you the total number of records.

0
$query= mysql_query($sqlstring,$this->db) or die('<h1>Unable to query database</h1>' . mysql_error());
echo mysql_num_rows($query);

mysql_num_rows is return the number of records in your table

Edited by rajarajan07: n/a

0

Thanks folks, I managed to get what I wanted. Below is the full code:

<pre><?php  
     class Database
{
	
	private $connected=false;
	private $user,$pass,$host;
	private $db;
	private $result=array();
        
	
	public function initialize($user, $pass, $host)
	{
	 $this->user=$user;
	 $this->pass=$pass;
	 $this->host=$host;
	 return $this;
		
    }
	
  function connect ($dbname)
  
  {
   $this->db=mysql_connect($this->host,$this->user,$this->pass) or
   die('<h1>Unable to connect to database</h1>' . mysql_error()); 
   mysql_select_db($dbname) or die('<h1>Unable to select database</h1>' . mysql_error());
   return $this;
  }
  
  function query ($sqlstring)
  {     $this->result = array(); //resets the array anytime a query is run
	$query= mysql_query($sqlstring,$this->db) or die('<h1>Unable to query database</h1>' . mysql_error());  
	while($record=mysql_fetch_assoc($query)){
		$this->result[]=$record;
	}
       // echo mysql_num_rows($query) . "<br />";// counts the total number of rows
	return $this->result;
  }
// An alternative to display numRows is to write a method

    function numRows($table)
    {
        $result = $this->query("select count(*) as totalRows from $table");
         return $result[0]['totalRows'];// it will start counting from the first array in the table
    }

  
}

$db = new Database();
print_r($db->initialize("root","project","localhost")->connect("mmsplus")->
        query("SELECT * FROM members"));
echo $db->numRows('members');

?></pre>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.