Re: why is PEAR::DB constantly initting connection?
| From: | Justin Patrin | Date: | Fri, 24 Mar 2006 15:52:06 +0000 |
| Subject: | Re: why is PEAR::DB constantly initting connection? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-23443@lists.php.net to get a copy of this message | ||
On 3/23/06, joseph <kmh496@kornet.net> wrote:
> bonjour,
>
> why does my DB object always send INIT DB to mysql server? i am using
> PHP 5.0.5 and
> * @version Release: 1.7.5
> * @link http://pear.php.net/package/DB
> */
>
>
> the problem caused by calls like the following -- very simple code,
> really ---- may be caused by my PEAR/DB.php wrapper functions (see
> below) . which are logically stupid, nevertheless, should i stop using
> pear and go to mysql_query which does not do INIT DB in my server logs?
> i need to run lots of queeries ( order of thousands at a shot) so ....
> now, the evidence is my mysql query log (shown below the code, also),
> please help me pinpoint the error................
>
> if (!$db) $db=db_connect();^M
> $this_userRS = db_query($db,$query);
> $this_user= $this_userRS->fetchRow();
> $top_bar_text = "these are old todo list entries by " .
> $browse_user_name . " (". $this_user[0] .")";
> $max_from_db_table = $this_user[0];
>
>
>
> i use functions like this :
>
>
> function db_connect() {
> global $err,$errstring, $dsn,$mysql_host,$mysql_db,$mysql_user,
> $mysql_password,$DEBUG_DICT_CONNECT,$options;
> // Retries connection to server 5 times.
> if ($DEBUG_DICT_CONNECT) echo "dsn = " . print_r($dsn) . "<br>";
> for ($i = 0; !(@$db = DB::connect($dsn,$options)) && $i < 5; $i++)
> { }
> if (DB::isError($db))
> {
> header("Location: http://" .
> $_SERVER['HTTP_HOST'] .
> "/index.php?cfile=err&query=" . $err . "&errstring='" .
> $errstring."'");
> exit;
> }
> else {
> db_query($db,"set names utf8");
> return $db;
> }
> } // end db_connect
>
> and
>
>
> function db_query(&$db,$query)
> {
> global $err,$errstring, $DEBUG_DICT, $tpf_admin_email;
> $result = $db->query($query);
> if (DB::isError($result))
> {
> exit;
> }
> else return $result;
> }
>
> function db_getAssoc(&$db,$query)
> {
> global $err,$errstring, $DEBUG_DICT, $_SERVER, $tpf_admin_email;
> $result = $db->getAssoc($query,false,array(),
> DB_FETCHMODE_ORDERED,true);
> if (DB::isError($result))
> {
> exit;
> }
> else return $result;
> }
>
>
> and my server query log shows up stuff like
>
>
> 373 Init DB utf8_explicit
> 373 Query select
> rss_id,rss_url,rss_name,rss_cache_time,rss_listnum,rss_listtype,from_lang from rss_info where
> rss_user = 'curious' and cid = '246' and from_lang='german'
> 373 Query select cid from rss_category where
> cname= 'misc' and from_lang='french'
> 373 Init DB utf8_explicit
> 373 Query select
> rss_id,rss_url,rss_name,rss_cache_time,rss_listnum,rss_listtype,from_lang from rss_info where
> rss_user = 'joe' and cid = '219' and from_lang='french'
> 373 Init DB utf8_explicit
> 373 Query select
> rss_id,rss_url,rss_name,rss_cache_time,rss_listnum,rss_listtype,from_lang from rss_info where
> rss_user = 'curious' and cid = '219' and from_lang='french'
>
You're not really using DB how you should be. You'r eusing far too
many globals and normal functions. Just use DB directly, don't use
functions like this.
Also try looking into PEAR::setErrorHandling().
Likely this "Init DB" thing is due to DB selecting the DB before every
query. If this is why it's happening don't bother to complain about
it, it's needed to correctly allow for multiple database connections.
You could also try using MDB2 and see if that does the same thing.
MDB2 is the supported DB layer now, DB is deprecated.
--
Justin Patrin