Posted on 2006-06-03
Last Modified: 2013-12-12

Is PEAR a safer way to access mysql database?
Does PEAR have a slower return then a regular database query?

How to I translate the simple queries into PEAR:

1.  $sql = mysql_query("SELECT * FROM x WHERE username='$username' AND id='$id'");
$ok = mysql_num_rows($sql);

2.  $sql2 = mysql_query("SELECT COUNT(*) as TOTALFOUND from inbox WHERE username='$username' and checked='n'"); print(mysql_result($my_table,0,"TOTALFOUND"));

3.  $insert=mysql_query ("INSERT INTO ib (username, fromuser)
VALUES ('$to','$from')") or die (mysql_error());

4.  $update=mysql_query("UPDATE c SET profile='$updatec' WHERE id='$id'") or die (mysql_error());

Question by:aboskoco
    LVL 49

    Expert Comment

    PEAR is a library of scripts, not a way of accessing a mysql database

    PEAR contains classes that provide ways of accessing databases like mysql.

    There are several classes, and pear can be slower then plain mysql_connect/query calls because of additional functionality.



    Author Comment

    I have been informed by others just the opposite.

    PEAR::DB, the database abstraction library is what has been mentioned to me.
    LVL 49

    Accepted Solution

    Yes, that is correct. But that is not what you question states. PEAR::DB is one of the classes in the PEAR package.

    The site lists however that PEAR:DB is superseded by PEAR:MDB2

    To run your sample queries you can use:

    // Create a valid MDB2 object named $mdb2
    // at the beginning of your program...
    require_once 'MDB2.php';

    $mdb2 =& MDB2::connect('mysql://username:password@localhost/databasename');
    if (PEAR::isError($mdb2)) {

    // Query 1
    $res =& $mdb2->query("SELECT * FROM x WHERE username='$username' AND id='$id'");
    $ok = $res->numRows();

    // Query 2
    $res =& $mdb2->query("SELECT COUNT(*) from inbox WHERE username='$username' and checked='n'");
    echo $res->fetchOne(0);

    // Query 3
    $affected =& $mdb2->exec("INSERT INTO ib (username, fromuser) VALUES ('$to','$from')");
    if (PEAR::isError($affected)) {

    // Query 4
    $affected=& $mdb2->query("UPDATE c SET profile='$updatec' WHERE id='$id'");
    if (PEAR::isError($affected)) {



    Author Comment

    thank you. Point well taken

    My main reason for wanting to learn pear is because it is supposedly far more hacker safe, is this correct?...

    require_once 'MDB2.php'; = what is this referring to?

    $row = mysql_fetch_array($res) - does this still apply or does pear use a separate way to create this array?

    LVL 49

    Expert Comment

    In general your server would have the PEAR package directory as part of its include path.

    So when you do require_once 'MDB2.php' it should load MDB2.php in the PEAR directory.

    I think you cannot use use mysql_fetch_array($res) because MDB2 doesn't return the result of mysql_query, but it creates its own result object, which is not depending on a specific database scheme. The MDB2 greatest power might just be that you can hook it to oracle, postgres, mysql without having to change all your code, except possibly your queries that use database-specific syntax.

    You can use:
    $row = $res->fetchRow();


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now