Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #8141 > unrolled thread

PEAR DB: "PHP Fatal error: Call to undefined method DB::query()"

Started byRobert S <robert.schmidli@gmail.com>
First post2013-01-29 01:03 -0800
Last post2013-01-29 07:58 -0500
Articles 5 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  PEAR DB: "PHP Fatal error:  Call to undefined method DB::query()" Robert S <robert.schmidli@gmail.com> - 2013-01-29 01:03 -0800
    Re: PEAR DB: "PHP Fatal error:  Call to undefined method DB::query()" Christoph Becker <cmbecker69@gmx.de> - 2013-01-29 10:19 +0100
      Re: PEAR DB: "PHP Fatal error:  Call to undefined method DB::query()" Robert S <robert.schmidli@gmail.com> - 2013-01-29 02:35 -0800
        Re: PEAR DB: "PHP Fatal error:  Call to undefined method DB::query()" Christoph Becker <cmbecker69@gmx.de> - 2013-01-29 12:20 +0100
    Re: PEAR DB: "PHP Fatal error:  Call to undefined method DB::query()" Jerry Stuckle <jstucklex@attglobal.net> - 2013-01-29 07:58 -0500

#8141 — PEAR DB: "PHP Fatal error: Call to undefined method DB::query()"

FromRobert S <robert.schmidli@gmail.com>
Date2013-01-29 01:03 -0800
SubjectPEAR DB: "PHP Fatal error: Call to undefined method DB::query()"
Message-ID<0a977961-c477-420e-9404-a9e027174b84@googlegroups.com>
I have the following code:

<?php
require_once( 'DB.php' );
$dsn = 'mysql://user:password@localhost/table';
$dbh = new DB();
$dbh->connect( $dsn );
if ( $dbh->isError( $dbh )) die ( $dbh->getMessage );
  $query = "SELECT * FROM Table ORDER BY Date LIMIT 1";
    $sth = $dbh->query( $query );
?>

I get:

PHP Fatal error:  Call to undefined method DB::query()

Can somebody tell me where I am going wrong?  This all worked using my old code, but I got a lot of warnings about non static methods being called statically:

$dbh = DB::connect( $dsn );
if ( DB::isError( $dbh )) die ( $dbh->getMessage );
$sth = $dbh->query( $query );

[toc] | [next] | [standalone]


#8142

FromChristoph Becker <cmbecker69@gmx.de>
Date2013-01-29 10:19 +0100
Message-ID<ke8474$362$1@speranza.aioe.org>
In reply to#8141
Robert S wrote:

> Can somebody tell me where I am going wrong?  This all worked using
> my old code, but I got a lot of warnings about non static methods
> being called statically:

Apparently DB::connect() doesn't return an instance of the DB class.  So
instead of

  $dbh->connect( $dsn );

you have to call

  $dbh = $dbh->connect( $dsn );

But probably it's better to stick with your old code, and declare static
methods as such in the DB class:

  public static function connect($dsn) {...}

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#8145

FromRobert S <robert.schmidli@gmail.com>
Date2013-01-29 02:35 -0800
Message-ID<570000a9-74fb-49f2-8197-3629e69b7477@googlegroups.com>
In reply to#8142
> 
>   $dbh = $dbh->connect( $dsn );
> 
That seems to work.  I still get messages like this:

PHP Strict Standards:  Non-static method DB::isError() should not be called statically, assuming $this from incompatible context in /usr/share/php/DB/common.php on line 1217, referer: http://blah ..

> But probably it's better to stick with your old code, and declare static
> methods as such in the DB class:
>   public static function connect($dsn) {...}
> 
Where do I declare this?  Is it in my code or in DB.php?

[toc] | [prev] | [next] | [standalone]


#8148

FromChristoph Becker <cmbecker69@gmx.de>
Date2013-01-29 12:20 +0100
Message-ID<ke8b8b$nik$1@speranza.aioe.org>
In reply to#8145
Robert S wrote:

> Where do I declare this?  Is it in my code or in DB.php?

It is in the file where class DB is defined (probably DB.php), which
seems to have been written for PHP 4, where the "static" keyword was not
allowed for class members.

You might consider to switch to another DB abstraction layer, e.g. PDO
(<http://php.net/manual/en/book.pdo.php>).

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#8149

FromJerry Stuckle <jstucklex@attglobal.net>
Date2013-01-29 07:58 -0500
Message-ID<ke8h18$5oj$1@dont-email.me>
In reply to#8141
On 1/29/2013 4:03 AM, Robert S wrote:
> I have the following code:
>
> <?php
> require_once( 'DB.php' );
> $dsn = 'mysql://user:password@localhost/table';
> $dbh = new DB();
> $dbh->connect( $dsn );
> if ( $dbh->isError( $dbh )) die ( $dbh->getMessage );
>    $query = "SELECT * FROM Table ORDER BY Date LIMIT 1";
>      $sth = $dbh->query( $query );
> ?>
>
> I get:
>
> PHP Fatal error:  Call to undefined method DB::query()
>
> Can somebody tell me where I am going wrong?  This all worked using my old code, but I got a lot of warnings about non static methods being called statically:
>
> $dbh = DB::connect( $dsn );
> if ( DB::isError( $dbh )) die ( $dbh->getMessage );
> $sth = $dbh->query( $query );
>

OK, it used to work but doesn't now.  What changed?

You do have some problems in your code.  For instance, you are not 
saving the object returned by the connect() method.  You can find an 
example at 
http://pear.php.net/manual/en/package.database.db.intro-query.php for 
the proper way to do a query. Also, the fact you had warnings earlier 
should have been a red flag.  They are there for a reason.

However, beware that the PEAR DB package has been replaced by the PEAR 
MDB2 package.  The former is no longer being maintained and should not 
be used.

Rather, I recommend you use a the PHP PDO class.  It will require some 
rewriting of your code, but you're going to have to do that anyway.

BTW - it's almost never a good idea to modify the code in packages.  It 
always leads to more work in the long run as you have to maintain the 
modified code as well as your own.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web