8 Replies - 937 Views - Last Post: 02 April 2013 - 10:34 PM Rate Topic: -----

#1 rimfaxe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-March 13

Why does I not get any result

Posted 01 April 2013 - 01:13 AM

Hello forum
As a newbee I copid and modified a PDO connection class from Kinetic Klinic (RayRay). I call it DBm and It works now in my mysqli modification. This is now used in another class film, which should be used to retrieve data from my film database.
phpmyadmin tells me that it holds 353 titles (using exactly the same query). my get_count method returns NULL.
Can a kind person please give me a hint as to what I am doing wrong?

The DBM class
<?php
/*
-----------------------
Mysqli connection class DBm

filename class.DBm.php
*/
class DBm {

    private static $_instance = array();
    
    static function getInstance ($db_name = 'gianelli2_dk') {
        if (! array_key_exists($db_name, self::$_instance)) {
            $db_host = 'gianelli2.dk.mysql';
            $db_user = 'gianelli2_dk';
            $db_psw = '****';
            self::$_instance[$db_name] = new mysqli($db_host, $db_user, $db_psw);                
            if (mysqli_connect_error()) {
                die('Connect Error ('.mysqli_connect_errno().')'.mysqli_connect_error());
            }
            return self::$_instance[$db_name];
        }
    }
}

The film class (relevant parts)
<?php
/*
-------------------------
Class for film table

filename: class.film.php
*/
require_once 'class.DBm.php';

class film {
  
    private static $dbConn = null;
  
    public function __construct () {
        self::initializeConnection();
    }
   
    private static function initializeConnection () {
        if (is_null(self::$dbConn)) {
            self::$dbConn = DBm::getInstance();
        }
    }      
  
 //------------------------------------
 //
 // Get methods
 
 
    public function get_count(){
        self::initializeConnection();
        $stmt = null;
        $sql = "SELECT COUNT(id)FROM film WHERE 1";
        $stmt = self::$dbConn->query($sql);
        if(!stmt) {
            /* error */
            echo 'Query = '.$sql.'<br>';
            echo 'Query Error :(/>/>'.$stmt->errno.' '.$stmt->error.')';            
        }
        return $stmt;
    }
} 


I test it with this little script
<?php
require_once 'class.DBm.php';
require_once 'class.film.php';
//
$db = new film();
$db->get_count();

echo "<br>----------------------------------------<br><br>";
echo "var_dump stmt<br><br>";
var_dump ($stmt);

echo "<br>----------------------------------------<br><br>";
echo "var_dump db<br><br>";
var_dump ($db);

echo "<br>----------------------------------------<br><br>";
?>


which returns stmt = NULL and db object(film)#1 (0) { }.

This post has been edited by Dormilich: 01 April 2013 - 02:56 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Why does I not get any result

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3614
  • View blog
  • Posts: 12,437
  • Joined: 12-December 12

Re: Why does I not get any result

Posted 01 April 2013 - 01:33 AM

$stmt isn't assigned a value in your test script and

$db->get_count();

doesn't assign the return result to anything.

I haven't checked for any other errors.
Was This Post Helpful? 2
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Why does I not get any result

Posted 01 April 2013 - 03:06 AM

$stmt = self::$dbConn->query($sql);
if(!stmt) {


What's missing on that second line? Look closely.

if(!stmt) {
    /* error */
    echo 'Query = '.$sql.'<br>';
    echo 'Query Error :(/>'.$stmt->errno.' '.$stmt->error.')';            
}


If the stmt variable is invalid, does it make any sense to try to use it in your error handling?
Was This Post Helpful? 1
  • +
  • -

#4 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3614
  • View blog
  • Posts: 12,437
  • Joined: 12-December 12

Re: Why does I not get any result

Posted 01 April 2013 - 03:10 AM

It should be if (!$stmt)
Was This Post Helpful? 1
  • +
  • -

#5 rimfaxe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-March 13

Re: Why does I not get any result

Posted 01 April 2013 - 06:10 AM

View Postandrewsw, on 01 April 2013 - 01:33 AM, said:

$stmt isn't assigned a value in your test script and

$db->get_count();

doesn't assign the return result to anything.

I haven't checked for any other errors.

Thank you, I have tried to make the method return num_rows (still in vain, though).
Was This Post Helpful? 0
  • +
  • -

#6 rimfaxe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-March 13

Re: Why does I not get any result

Posted 01 April 2013 - 06:30 AM

View PostJackOfAllTrades, on 01 April 2013 - 03:06 AM, said:

$stmt = self::$dbConn->query($sql);
if(!stmt) {


What's missing on that second line? Look closely.

if(!stmt) {
    /* error */
    echo 'Query = '.$sql.'<br>';
    echo 'Query Error :(/>/>'.$stmt->errno.' '.$stmt->error.')';            
}


If the stmt variable is invalid, does it make any sense to try to use it in your error handling?

How embarrassing, leaving out the $sign. Have made some changes, but are till facing no output and no errormessages. will keep trying, thank you for your reply.
Was This Post Helpful? 0
  • +
  • -

#7 rimfaxe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-March 13

Re: Why does I not get any result

Posted 01 April 2013 - 10:08 PM

I now have made a working error trapping and get the right result within the method. The SQL had to be changed in order to work properly (php sql <> mysql SQL?).
<?php
/*
-------------------------
Class for film table

filename: class.film.php
*/
require_once 'class.DBm.php';

class film {
  
    private static $dbConn = null;
    public $count = null;
    public $field_key = '';
    
    public function __construct () {
        self::initializeConnection();
    }
   
    private static function initializeConnection () {
        if (is_null(self::$dbConn)) {
            self::$dbConn = DBm::getInstance();
        }
    }      
 //------------------------------------
 //
 // Get methods
    public function getCount () {
        return $this->count;
    }
 //------------------------------------
 //
 // Set methods        
        public function setfield_key ($field_key) {
        $this->field_key = $field_key;
    }
 
    public function count_rows(){
        self::initializeConnection();
//        $film = NULL;
        $sql = "SELECT COUNT(*) as c FROM film"; 
        $stmt = self::$dbConn->query($sql);
        if (self::$dbConn->error) {
            // error 
            echo 'Query = '.$sql.'<br>';
            echo 'Query Error : ('.self::$dbConn->errno.') '.self::$dbConn->error.'<br><hr>';            
        }        
        //succes
        $res = $stmt->fetch_object()->c;
        $stmt->free();
        self::$dbConn->close();
//        printf("Number of records (from within method get_count() ): %d\n", $res); 
        return $this->count=$res;
    }
} 

Thank you all for your kind help - hope to be able to return help to someone in need sometime in the future.
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,546
  • Joined: 23-August 08

Re: Why does I not get any result

Posted 02 April 2013 - 03:32 AM

Quote

The SQL had to be changed in order to work properly (php sql <> mysql SQL?).


There is no such thing as "php sql". If you're using MySQL, the SQL you execute should conform to MySQL's implementation, if you're using Oracle, then it should conform to theirs, et cetera.
Was This Post Helpful? 0
  • +
  • -

#9 rimfaxe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-March 13

Re: Why does I not get any result

Posted 02 April 2013 - 10:34 PM

View PostJackOfAllTrades, on 02 April 2013 - 03:32 AM, said:

Quote

The SQL had to be changed in order to work properly (php sql <> mysql SQL?).


There is no such thing as "php sql". If you're using MySQL, the SQL you execute should conform to MySQL's implementation, if you're using Oracle, then it should conform to theirs, et cetera.


I ran an query from phpmyadmin "SELECT COUNT (id) WHERE 1". This failed when executing from php.
Reason: When in phpmyadmin, the table was selected. When executing from php the table of cause needs to be selected, hence the change.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1