[PHP] [OOP] Class to handle PEAR::DB

2004-11-18 Thread valerie17
I'm making a counter using DB,

1  ?php
2  require_once 'DB.php';
3
4  $sql_type   = 'mysql';
5  $sql_user   = 'root';
6  $sql_pass   = '';
7  $sql_host   = 'localhost';
8  $sql_db = 'valerie';
9
10 $dsn=
$sql_type://$sql_user:[EMAIL PROTECTED]/$sql_db;
11 $db = DB::connect($dsn);
12
13 // autoExecute config
14 $table_name = 'counter';
15 $fields_values  = array('counter' = $counter++);
16 $where  = page = 'index';
17
18 $result = $db-query(LOCK TABLES counter WRITE);
19 if ( DB::isError($result) ) {
20 die($result-getMessage());
21 }
22
23 $sql= SELECT counter FROM $table_name WHERE $where;
24 $result = $db-query($sql);
25 if ( DB::isError($result) ) {
26 die($result-getMessage());
27 }
28 list($counter)  = $result-fetchRow();
29
30 // update counter
31 $result = $db-autoExecute($table_name, $fields_values,
DB_AUTOQUERY_UPDATE, $where);
32 if ( DB::isError($result) ) {
33 die($result-getMessage());
34 }
35
36 $result = $db-query(UNLOCK TABLES);
37 if ( DB::isError($result) ) {
38 die($result-getMessage());
39 }
40
41 die;
42 ?

it works well, but I don't think it's efficient calling DB::isError
everytime, so (inspired by Advanced PHP Programming book by Mr. George
Schlossnagle) I made a class (PHP 4.3.9) to handle it . (instead of using
functions and must call boring 'global' at the top of each function)

1  ?php
2  /**
3   * PEAR DB
4   */
5  require_once 'DB.php';
6
7  class DBku
8  {
9  // protected
10 var $type;
11 var $user;
12 var $pass;
13 var $host;
14 var $db;
15 var $dbhandler;
16
17 // {{{
18 // public
19 function __construct($type, $user, $pass, $host, $db)
20 {
21 $this-type =  $type;
22 $this-user =  $user;
23 $this-pass =  $pass;
24 $this-host =  $host;
25 $this-db   =  $db;
26 }
27 // }}}
28
29 // {{{
30 // public
31 function connect()
32 {
33 $this-dbhandler =
DB::connect($this-type,://,$this-user,:,$this-pass,@,$this-host,/
,$this-db);
34 $this-error($this-dbhandler);
35 }
36 // }}}
37
38 // {{{
39 // public
40 function execute($query)
41 {
42 if ( !$this-dbhandler ) {
43 $this-connect();
44 }
45 $ret = $this-dbhandler-query($query);
46 $this-error($ret);
47 }
48 // }}}
49
50 // {{{
51 // protected
52 function error($er_value)
53 {
54 if (DB::isError($er_value)) {
55 echo 'Standard Message: ' . $er_value-getMessage() .
\n;
56 echo 'Standard Code: ' . $er_value-getCode() . \n;
57 echo 'DBMS/User Message: ' . $er_value-getUserInfo() .
\n;
58 echo 'DBMS/Debug Message: ' . $er_value-getDebugInfo() .
\n;
59 die;
60 }
61 }
62 // }}}
63 }
64
65 $sql = new DBku('mysql', 'root', '', 'localhost', 'valerie');
66 var_dump($sql);
67 $sql-execute('SELECT * FROM country');
68 die;
69 ?

my problem is : everytime I execute this file in CLI mode I always got this
:

E:\php\pearphp config_valerie4.inc.php
object(DBku)#1 (6) {
  [type]=
  string(5) mysql
  [user]=
  string(4) root
  [pass]=
  string(0) 
  [host]=
  string(9) localhost
  [db]=
  string(7) valerie
  [dbhandler]=
  NULL
}
Standard Message: DB Error: no database selected
Standard Code: -14
DBMS/User Message: SELECT * FROM country [nativecode=1046 ** No Database
Selected]
DBMS/Debug Message: SELECT * FROM country [nativecode=1046 ** No Database
Selected]

How come the message said I didn't select database ??

I also throw this thread to php-general mailing lists because when somebody
talked about OOP then many arguments would come from many people there and I
just enjoy to hear (read) it :)

So, Opinions (good or bad things), Suggestions, Corrections to my code
(especially idea to make my class shorter and would be compatible in PHP4 
5) would be very welcome.

Thank you.

Valérie Delon
étudiante

--
On a besoin d'apprendre beacoup plus
Quelqu'un croit c'est trop
Mais j'crois que ce n'est pas assez

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] [OOP] Class to handle PEAR::DB

2004-11-18 Thread Jason Wong
On Thursday 18 November 2004 17:05, [EMAIL PROTECTED] wrote:

 my problem is : everytime I execute this file in CLI mode I always got this

[...]

 Standard Message: DB Error: no database selected
 Standard Code: -14
 DBMS/User Message: SELECT * FROM country [nativecode=1046 ** No Database
 Selected]
 DBMS/Debug Message: SELECT * FROM country [nativecode=1046 ** No Database
 Selected]

 How come the message said I didn't select database ??

Does this work when executed via a webserver? If so, have you checked that you 
have MySQL support compiled into your CLI php?

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
For a moment, nothing happened. Then, after a second or 
so, nothing continued to happen. 
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] [OOP] Class to handle PEAR::DB

2004-11-18 Thread valerie17
 object(DBku)#1 (6) { [type]= string(5) mysql [user]= string(4)
root [pass]= string(0)  [host]= string(9) localhost [db]=
string(7) valerie [dbhandler]= NULL } Standard Message: DB Error: no
database selected Standard Code: -14 DBMS/User Message: SELECT * FROM
country [nativecode=1046 ** No Database Selected] DBMS/Debug Message: SELECT
* FROM country [nativecode=1046 ** No Database Selected]

of course it would return the same otherwise it'll have no BR via web
browser . And yes, my previous projects worked very well and I do have MySql
support for my PHP 5.0.1. Isn't it strange ? Or it's just the logic of my
OOP code go wrong ?

Valérie Delon
étudiante

--
On a besoin d'apprendre beacoup plus
Quelqu'un croit c'est trop
Mais j'crois que ce n'est pas assez


From: Jason Wong [EMAIL PROTECTED]
Date: Thu, 18 Nov 2004 19:06:56 +0800
Subject: Re: [PHP] [OOP] Class to handle PEAR::DB

On Thursday 18 November 2004 17:05, [EMAIL PROTECTED] wrote:

 my problem is : everytime I execute this file in CLI mode I always got
this

[...]

 Standard Message: DB Error: no database selected
 Standard Code: -14
 DBMS/User Message: SELECT * FROM country [nativecode=1046 ** No Database
 Selected]
 DBMS/Debug Message: SELECT * FROM country [nativecode=1046 ** No Database
 Selected]

 How come the message said I didn't select database ??

Does this work when executed via a webserver? If so, have you checked that
you
have MySQL support compiled into your CLI php?

--
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
For a moment, nothing happened. Then, after a second or
so, nothing continued to happen. 
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php