Link to home
Start Free TrialLog in
Avatar of mafiq
mafiq

asked on

PearDB

Anyone with an idea of how I can get the Pager up and running on a Linux platform using

PHP/MySql. I want paginated results from mysql database.
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mafiq
mafiq

ASKER

Yes, I have already downloaded the file named

DB Pager-0[1].7.tar but how to start using it and the version of php it supports is a problem.

I am new to php
Avatar of mafiq

ASKER

I have extracted the Pager.php file into same folder with my php file with following code:

<?php
 require 'Pager.php';
 $db = mysql_connect("localhost", "root");
 mysql_select_db("uzexams",$db) or die();
 $sql = "select * from faculty";
 $result = mysql_query($sql);
 $limit = 10;
 $from = 50;
 $pager = new Pager ($result, $from, $limit);
 $data = $pager->build();
 if (DB::isError($data))
 {

  die (DB::errorMessage($data));
 }
 if (!$data)
 {
  die ('There were no results');

  }
  echo "<html><body>";
  echo $data['numrows'] . " Results found";
  echo 'Page '. $data['current'] . " of " . $data['numpages'];
  echo $data['limit'] . " results per page";
  echo "<a href='".$PHP_SELF."'"."?from='".$data['next']."'".">'".$data['remain']."'"."Next->"."</a>";
  foreach ($data['pages'] as $page => $start_row)
  {
    echo "<a href='$PHP_SELF?from=$start_row'>$page</a>";
   }
   while ($row = $pager->fetchRow(DB_FETCHMODE_ASSOC))
   {
    echo $row['id'] . "\n";
    }
    echo '</body></html>';
 ?>

When I try running it, the following error results:

Fatal error: Cannot instantiate non-existent class: pager in /var/www/html/PageTest.php on line 9
Try

require "./Pager.php";

You may find that your include_path string setting is not standard.

From the PHP manual ...

include_path string
Specifies a list of directories where the require(), include() and fopen_with_path() functions look for files. The format is like the system's PATH environment variable: a list of directories separated with a colon in UNIX or semicolon in Windows. Example 3-3. UNIX include_path

include_path=.:/home/httpd/php-lib
 
 
Example 3-4. Windows include_path

include_path=".;c:\www\phplib"
 
 
The default value for this directive is . (only the current directory).




To see that the setting is try ...

<?php phpinfo(); ?>

as info.php

This will show you all your system's settings for PHP.
Avatar of mafiq

ASKER

I have tried using your info.php and the following is for

include_path

.:/php/includes:/usr/share/php

What should I use then



That says that you can use the current directory

./

and 2 other directories

/php/includes

and

/usr/share/php

So. You can use ./Pager.php

Try this.

Create a new file in the same directory as Pager.php

<?php echo "I'm here!"; ?>

and call this test.php

Try calling test.php directly from your browser. Then replace the require statement you already have with

require "./test.php";

and then reload your script.

You should get the message at the top and a load of errors relating to the missing class.


Oh! Check the case too. I'm sure that is important on *IX systems.


Regards,

Richard.
Avatar of mafiq

ASKER

When I used the test.php it worked out fine and printed
I'm Here but when I replaced with Pager.php
the following error is generated:
Fatal error: Cannot instantiate non-existent class: pager in /var/www/html/PageTest.php on line 9


isn't there something I should have installed?


I can't believe this, but ...


class DB_Pager extends PEAR

is in Pager.php.

And the example code on the main site is wrong!!!!

Try ...

$pager = new DB_Pager ($result, $from, $limit);

Avatar of mafiq

ASKER

The error is now being generated in Pager.php.


Fatal error: Call to a member function on a non-object in /var/www/html/Pager.php on line 98
Avatar of mafiq

ASKER

The error is now being generated in Pager.php.


Fatal error: Call to a member function on a non-object in /var/www/html/Pager.php on line 98

Don't I need

PEAR.php
and DB.php
Of course. You are using a PEAR_DB pager!
These are normally part of a PHP installation. If not, then you will need to get them.

I use a Windows environment and they come with PHP. If you are using a *IX environment, I'm not sure how to get it, but it involves CVS (I think).
Avatar of mafiq

ASKER

thanks

will try it out
Avatar of mafiq

ASKER

I have the two files

in /usr/share/php folder but can't figure out why it's not working!
Avatar of mafiq

ASKER

The following is the part code from Pager.php. I have labelled the line generating errors Lin 98.

class DB_Pager extends PEAR
{

    /**
    * Constructor
    *
    * @param object $res  A DB_result object from Pear_DB
    * @param int    $from  The row to start fetching
    * @param int    $limit  How many results per page
    * @param int    $numrows Pager will automatically
    *    find this param if is not given. If your Pear_DB backend extension
    *    doesn't support numrows(), you can manually calculate it
    *    and supply later to the constructor
    * @deprecated
    */
    function DB_Pager (&$res, $from, $limit, $numrows = null)
    {
        $this->res = $res;
        $this->from = $from;
        $this->limit = $limit;
        $this->numrows = $numrows;
    }

    /**
    * Calculates all the data needed by Pager to work
    *
    * @return mixed An assoc array with all the data (see getData)
    *    or DB_Error on error
    * @see DB_Pager::getData
    * @deprecated
    */
    function build()
    {
        // if there is no numrows given, calculate it
        if ($this->numrows === null) {
            $this->numrows = $this->res->numrows();**Lin 98
            if (DB::isError($this->numrows)) {
                return $this->numrows;
            }
        }
        $data = $this->getData($this->from, $this->limit, $this->numrows);
        if (DB::isError($data)) {
            return $data;
        }
        $this->current = $this->from - 1;
        $this->top = $data['to'];
        return $data;
    }
You should be able to comment out the Build function as it is no longer in use (according to the notes in the file).
With 35 points we cannot make split.
Ha. Only I commented anyway. So me then!