Categories
php

Php Function To Count No of Rows In a Table

Occasionally you bump into a situation where you have to count the no of rows in a table in database.

There is one quick n dirty method to do that..

$noofrows = mysql_num_rows(mysql_query("select * from table"));

This method is inefficient and wont show errors while executing.

Alternatively you can use a  function which returns the no of rows in the table.. it takes table name and db connection handle as input and returns the row count or number of rows present in that table. Most of the time i use this function in pagination scripts.

function getnoofrows($table,$con){
  if($result = mysql_query("select count(*) as rowcount from table",$con)){
    $row = mysql_fetch_array($result);
    return $row['rowcount'];
  } else {
    //show MySQL error
    die(mysql_error());
  }
}

usage:

$noofrows = getnoofrows($table,$con);
Categories
php

Singleton Database Class for Beginners

What is A Singleton Class?

Singleton class is a class for which there exists only one object instance during the run time of the script (in php).

What is the use of singleton class?

It prevents the wastage of resources, if you have already established a connection to the database it retrieves that instance which already exists and reuses it. So there is no need to open a new connection.

How singleton class works?
First it searches for any object instance in the the run time of the script and returns it. If there is no object instance of this class it creates a new one…

Here is the database class which i normally use in my projects..
[php]
class Database{
// Store the single instance of Database
private static $instance;
private static $con;
private function __construct() {
//setup database.
//echo “constructor called”;

// use your server name , sql username and password db name here

$this->con=mysql_connect (“host”, “user”, “password”);
mysql_select_db (“database”,$this->con);
}
public static function getInstance()
{
if (!self::$instance)
{
self::$instance = new Database();
}

return self::$instance;
}
public static function closeInstance()
{
if (!(!self::$instance))
{
$inst = self::$instance;
mysql_close($inst->con);
}
}
// Function to run query
public function query($sql)
{
//echo “query run”;
return mysql_query($sql,$this->con);
}
}

[/php]
Usage:
Call a class using

[php]$db = Database::getInstance();[/php]

Query database

[php]$result = $db->query(‘sql statement here’);[/php]

Close Db Connection

[php]Database::closeInstance();[/php]

Thats all for now about the database class.. you no need to know how it works to use it but you need to study if you want to develop this.

Categories
php

PHP Function to list contents of a directory

Many times you need to get the list of contents of a directory in php scripts. You can get list of files and directories in a directory by using following function. It returns the list of items as an array.
[php]
function getdirlist($dir){

return array_diff( scandir( $dir ), Array( “.”, “..” ) );

}

[/php]
This function works in php5 environment only.

But if there are too many files in the folder you can use the below function to list each of them.

[php]

function getlist($directory){
$results = array();

// create a handler for the directory
//$directory=”.”;
$handler = opendir($directory);

// keep going until all files in directory have been read
while ($file = readdir($handler)) {

// if $file isn’t this directory or its parent,
// add it to the results array
if ($file != ‘.’ && $file != ‘..’){
$results[] = $file;
}
}
// tidy up: close the handler
closedir($handler);

return $results;

}

[/php]