Connecting Tech Pros Worldwide Forums | Help | Site Map

can't see the error (PHP5)

Martin Slingsby
Guest
 
Posts: n/a
#1: Jul 17 '05
Anyone who can decipher this error message? What is PHP trying to tell me?

exception 'MysqlException'
in /var/www/localhost/htdocs/phenixsp/include/DB.inc:72 Stack trace:
#0 /var/www/localhost/htdocs/phenixsp/include/DB.inc(58):
DB_MysqlStatement->__construct(NULL, 'SELECT * FROM u...')
#1 /var/www/localhost/htdocs/phenixsp/login.php(21):
DB_Mysql->prepare('SELECT * FROM u...') #2 {main}

The code is:
// login.php
<?php

// DB.inc handles all database connectivity and queries
include('include/DB.inc');
include('include/Conf.inc');

// rename variables to simplify further code
$user = $_GET['usr'];
$pass = $_GET['pass'];

// catch early error on part of the user
// you can't leave either or both variable empty in the login process

if(isset($user) && isset($pass)){

// do something to check credentials

$dbh = new DB_Mysql('$DBUSER', '$DBPASS', '$HOST', '$DB');
$newquery = "SELECT * FROM users WHERE name = :1";
try{
$stmt = $dbh->prepare($newquery); //line 21
$stmt->execute($user);

}
catch (MysqlException $e) {
print $e;

}

//DB.inc - starting on line 54

public function prepare($query){
if(!$this-dbh){
$this->connect();
}
return new DB_MysqlStatement($this->dbh, $query);
}
}

class DB_MysqlStatement {
protected $result;
protected $binds;
public $query;
public $dbh;

public function __construct($dbh, $query){
$this->query = $query;
$this->dbh = $dbh;
if(!is_resource($dbh)){
throw new MysqlException("Not a valid database connection");
}
}
}



Andy Hassall
Guest
 
Posts: n/a
#2: Jul 17 '05

re: can't see the error (PHP5)


On Sat, 18 Sep 2004 14:20:26 GMT, Martin Slingsby <Keizer.S@gmail.com> wrote:
[color=blue]
>Anyone who can decipher this error message? What is PHP trying to tell me?
>
>exception 'MysqlException'
>in /var/www/localhost/htdocs/phenixsp/include/DB.inc:72 Stack trace:
>#0 /var/www/localhost/htdocs/phenixsp/include/DB.inc(58):
>DB_MysqlStatement->__construct(NULL, 'SELECT * FROM u...')[/color]

Here's the big clue, the NULL.
[color=blue]
>#1 /var/www/localhost/htdocs/phenixsp/login.php(21):
>DB_Mysql->prepare('SELECT * FROM u...') #2 {main}
>
> $dbh = new DB_Mysql('$DBUSER', '$DBPASS', '$HOST', '$DB');[/color]

Single quotes? It's unlikely the database host is the literal string $HOST,
etc.

So this probably failed, but you didn't catch any exceptions or check for
errors. Regardless, you carry on:
[color=blue]
> $newquery = "SELECT * FROM users WHERE name = :1";
> try{
> $stmt = $dbh->prepare($newquery); //line 21[/color]

$dbh is at best in some sort of invalid, disconnected state so this will fail.
[color=blue]
>class DB_MysqlStatement {
> protected $result;
> protected $binds;
> public $query;
> public $dbh;
>
> public function __construct($dbh, $query){[/color]

The backtrace said the first parameter, $dbh, was NULL.
[color=blue]
> $this->query = $query;
> $this->dbh = $dbh;
> if(!is_resource($dbh)){[/color]

NULL is not a resource so:
[color=blue]
> throw new MysqlException("Not a valid database connection");[/color]

You get an exception thrown.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Closed Thread