[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 455
  • Last Modified:

PEAR

Hello,

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());

Thanks!!!
0
aboskoco
Asked:
aboskoco
  • 3
  • 2
1 Solution
 
RoonaanCommented:
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.

-r-

0
 
aboskocoAuthor Commented:
I have been informed by others just the opposite.

PEAR::DB, the database abstraction library is what has been mentioned to me.
0
 
RoonaanCommented:
Yes, that is correct. But that is not what you question states. PEAR::DB is one of the classes in the PEAR package.

The pear.php.net site lists however that PEAR:DB is superseded by PEAR:MDB2

To run your sample queries you can use:

<?php
// 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)) {
    die($mdb2->getMessage());
}

// 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)) {
    die($affected->getMessage());
}

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

$mdb2->disconnect();
?>

-r-
0
 
aboskocoAuthor Commented:
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?



0
 
RoonaanCommented:
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();

-r-
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now