Home » Php » PEAR extensions – PHP

PEAR extensions – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

Hi,

I’ve just recently starting getting into and learning PEAR, but one thing I haven’t come accross yet is using PEAR to insert data into a DB… can it be used for this? and also what are your views on it’s cache function?

A(Answer):

PEAR itself does not help you in programming, it’s a tool to provide you with additional PHP software (functions, classes, etc.).

although there are PEAR modules, that can do DB handling (MDB2*), PHP has powerful DB handlers itself (PDO and (esp. for MySQL) MySQLi)

* – last time I was trying MDB2, it was written for PHP 4 (compatible), which constantly threw E_STRICTs

A(Answer):

can i intergrate PDO & PEAR cache… my aim is to abstract data from a db as fast and as clean as possible but cache certain sql queries so getting the data is faster and uses less memory in the db server

A(Answer):

… cache certain sql queries …

that’s what Prepared Statements do.

the PDO-Guy

A(Answer):

ok last question can i use pdo for db abstraction as well just like pear db does? if i can use both then that would be perfect

A(Answer):

PDO *is* Database Abstraction.

A(Answer):

ok i’ve done a few tests selecting and inserting data using both pear and pdo… they are practically the same and work just as fast as each other… only difference for me is pdo doesn’t need the require_once statement as pear does to include functions and classes to work with.

but pear has a cache function, i’ve looked everywhere for a cache function pdo uses or even how to use it with query_cache… can you point me in the right direction?

A(Answer):

does query_cache is supported by all databases, that are supported by PDO?

A(Answer):

sorry this page went blank and when i refreshed it came back with to posts

A(Answer):

from what i have read mysql db has a query_cache function that once you switch it on everything is cached incase of repeat queries. if a query is repeated it will go to the cache before it processes anything and if it exists it will run from the cache and save memory and time. ive read forums and blogs to see if pdo has its own cache or is useable with query_cache but all i’ve seen is people in the same boat as me

A(Answer):

PDO is intended to use different DBs, if only MySQL and PGSQL have that cache option, I doubt the PHP developers would put that in.

but actually, I can’t imagine a scenario, where you would profit from the query_cache, I prefer to break it down and use Prepared Statements (because I’m too lazy to escape the input)

A(Answer):

ok so prepared statements work exactly the same way as a cache and does hog so much memory and time on a db server? if that’s so then i guess it’s the best and most simplest thing to do and use

A(Answer):

ok so prepared statements work exactly the same way as a cache and does hog so much memory and time on a db server?

no. Prepared Statements are a kind of SQL template that is saved on the database. once that is done, you only transfer data*.

* – and it’s incredibly safe against SQL Injection.

A(Answer):

sorry… *doesn’t

A(Answer):

ok, i understand pdo and now prepared statements… now for the last question… with PDO and preapred statements is there any need to cache the queries?

A(Answer):

that depends on your queries. rule of thumb: no duplicate data—no improvement through cache, duplicate data—can be optimized.

A(Answer):

i’ve basically designed and built my own social networking platform (taken a step up from the last time we spoke) using normal queries even for small things like a profile image took up alot of memory when there was alot of users testing for me.

I started to optimise this by testing with different db abstractions PEAR and PDO being my most favourable. they work very well but with pear another function that helps speed up the process cleanly is to use its cache function.

there are queries for profile comments, status updates, image comments, friends list, user information, event information and group information. it’s not alot but as it will loop through queried data so it lists it takes up alot with the loop.

i used query_cache for the main part of it but I want to use a file system cache instead as it is alot quicker and works away from my db server. pear has this function but pdo doesnt… it would be quicker if it did but if not then i will try mix pear cache with pdo

A(Answer):

PEAR is not a database abstraction.

A(Answer):

pear DB is… sorry this is what i used as well as PDO

A(Answer):

You should find PDO is quicker as it is a compiled C PHP extension. PEAR’s DB, on the other hand, is written in PHP.

A(Answer):

i have found it alot quicker… i have a question about PDO and a if loop should i post it in a new question or will you allow me to do it here?

A(Answer):

Here will do 🙂

A(Answer):

its a simple one… im still looking within the php manual about it.

I want to connect to a db using PDO and execute a prepared statement but once its executed i want to use a if loop to check if there is any data within the table if there is it goes on to display the data if not then echo a line of text here is my code… i’ve completely watered it down because its long. what function or method do I use within the if loop to check if there is data within the var $dataA


$connA = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$connA->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlA = "SELECT * FROM eventComments WHERE comOrigin = '{$userName}' AND comOrder = '1' ORDER BY dateAdded DESC";
$dataA = $connA->prepare($sqlA);
$dataA->execute();
if (***){
foreach ($dataA as $originalPost)
{
//display data
}
} else {
echo '<span style="text-align:left; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; font-weight:bold;">There are currently no comments!</span>';
}

A(Answer):

I believe you’ll want the PDOStatement::fetch method.

Edit: I believe I misunderstood. Do you want thePDOStatement::rowCount method?


if($stmt->rowCount() > 0) {
// Do loop
}

A(Answer):

row count will be the one…. thanks markus

A(Answer):

you could also try PDOStatement::fetchAll(), which gives you an Array.

A(Answer):

thanks dormilich… i’ve actually tried it and it works well

A(Answer):

the only inconvenient thing about PDO is the amount of fetch methods …

A(Answer):

lol yeah i noticed that… some seem like they are not needed the more important one for me are the associative array… as long as im able to get data from strings in a db into a associative array thats fine… i am using explode() to do this as well

A(Answer):

I can use everyone of them …

A(Answer):

im sure i will have to as i keep improving this social platform… thanks for all the help dormilich and markus… always appreciated

A(Answer):

Come again… 🙂