Posts Tagged ‘pear’

Using PEAR Pager with PHP PDO

This post will be addressing 2 things.

  1. How to use PEAR’s Pager class with PHP PDO
  2. Addressing PDO bug #44639 (http://bugs.php.net/bug.php?id=44639)

Two queries will be used for this task. The first query, you will count the rows of the recordset. The second query is the same as the first but it will have the LIMIT – this is what will be used to display the data on each page.

Let’s say we have the following table.

CREATE TABLE IF NOT EXISTS`blog`
(
`blog_id` int(11) not null auto_increment,
`blog_text` varchar(4000) not null default ”,
PRIMARY KEY `pk_blog_id` (`blog_id`)
) ENGINE=INNODB;

Now we’ll query the database and get the row count using PDO.

<?php

require_once(‘file-with-pdo-db-connection.php’);

$sql = “SELECT * FROM `blog`”;
$stmt = $dbh->prepare($sql);

if ($stmt->execute())
{
// begin pager
require_once(‘Pager/Pager.php’);
$params = array
(
‘totalItems’ => $stmt->rowCount,
‘perPage’ => 10,
‘delta’ => 5,
‘mode’ => ‘Sliding’
);

$pager =& Pager::factory($params);

$links = $pager->getLinks();
echo $links['all'];

// offset setup
list($from, $to) = $pager->getOffsetByPageId();
$from = $from – 1;
$perPage = $params['perPage'];

// 2nd query based on 1st with LIMIT – this will be displaying data per page
$stmt2 = $db->prepare($sql.’ LIMIT :from, :perPage’);

// address bug 44639 – forces the variables to have the property of integer instead of string so no quotes will surround it
$stmt2->bindValue(‘:from’, $from, PDO::PARAM_INT);
$stmt2->bindValue(‘:perPage’, $perPage, PDO::PARAM_INT);
$stmt2->execute();

while ($row_blog = $stmt2->fetch())
{
echo ‘<div>’.$row_blog['blog_text'].’</div>’;
}

}

$dbh = null;

?>

If you don’t use the bindValue, you will get an error in your query. Your second query would look something like this… SELECT * FROM `blog` LIMIT ’0′, ’10′ and the error would occur right after LIMIT. You cannot have quotes surrounding the numbers. It took a few hours of Googling but found the solution.

If you want to read more about PEAR Pager, visit http://pear.php.net/package/Pager/docs.

Here’s a tutorial I found that helped me out a lot – http://www.codediesel.com/php/simple-pagination-in-php/.

  • Share/Bookmark

Installing PEAR on Godaddy shared accounts

I recently found out how to install PEAR on a Godaddy shared hosting account.

  1. Visit http://pear.php.net/go-pear. Save the text displayed onto your desktop and name it go-pear.php. Upload this to your server. If you’re hosting multiple domains on the account, it’s best that you should upload this file in your root folder. Run this file on your browser and follow the on-screen instructions.
  2. After completing the installation, it is time to edit your php.ini file. If you are running PHP4, there should be a file in the root directory called php.ini. If you are running PHP5, there should be a file in the root directory called php5.ini. If not, create one and add the following: include_path = “.:/usr/local/php5/lib/php:/home/content/s/a/m/sample/html/PEAR”. Keep in mind that /s/a/m/sample is just an example. Doing this will ensure that every page, you create, will look in the PEAR directory so that you it will use the installed packages.
  3. This installation will include Pear_Frontend_Web which is the web-based admin interface. It may have created an index.php file in the directory where PEAR is installed (root). If not you can get a copy from PEAR/docs/PEAR_Frontend_Web/docs/index.php.txt. I would suggest creating a folder called pear_admin in the root directory and storing this file there. If you didn’t install PEAR in the root directory, you may need to edit this file accordingly.
  4. You will need to create a .htaccess and .htpasswd file in the same directory as the index.php file. An example of how the .htaccess file should look as follows:
    AuthUserFile /home/content/s/a/m/sample/html/pear_admin/.htpasswd
    AuthType Basic
    AuthName “Web-based PEAR Frontend”
    Require valid-user

    An example of how the .htpasswd file should look as follows:


    admin:cGyUX9QugYMgE

    This will create “admin” as the user name and “password” as the password. You can generate your own by going to this link – http://www.htaccesstools.com/htpasswd-generator/

    Be aware that files beginning with a dot are invisible. You may have to edit your settings on the FTP app you’re using so that you can see them.

Once the files are created and saved, you can now go to http://your-domain.com/pear_admin/index.php. It will ask for the user name and password. Once you are logged in, you can now manage Pear via web browser. That’s it! Now you can run PEAR on a shared account from Godaddy. One less complaint :)

  • Share/Bookmark

Nevermind on switching

After playing with a paid version Hostmonster, it didn’t meet my expectations. While using the demo I got an idea of the features and services they provide, it still lacked what I was looking for/expected.

  • Â SSH, though available, you cannot connect from your computer. You still had to log into cPanel and use its SSH interface. Not only that, but you have to provide a picture to Hostmonster in order for it to be enabled.
  • Pear libraries can be installed, but only stable versions. There were a couple of Pear libraries I’d like to use but are in beta (Spreadsheet_Excel_Writer and Calendar just to name a few).

Those two are the major things that stuck out. It wasn’t worth the trouble of switching if I couldn’t get those 2 to work the I wanted it to. So I called them up and cancelled the account. The experience was great! They asked for the domain and they refunded the full amount, no problems. If I didn’t already have a host, I may have gone with Hostmonster just because of the experience. I also used their online chat to ask questions before I got an account and during. They were helpful and efficient.

  • Share/Bookmark
Categories