1,105,177 Community Members

Fatal error: Call to a member function query() on a non-object,$mysql->query($query)

Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

HI,..i've been working on this simple project that i need to submit it tomorrow.
I really in rushing a little bit.

But while i working on this oop project, my program give the strange little fatal error about my query which is this

Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\listnote\index.php on line 25


I really sure that i wrote everything down correctly already, so that i really dont understand why did this error appear :(

Below here are the code :

This is my database class which i name it db.php

?php
//my database class
class Db {

    // make sure we dont connnect database to every function
    public $mysql;

    function __contruct(){
        $this->mysql = new mysqli('localhost','root','password','db') or die ("problem");
    }
?>

and here is my main program, index.php

<div id="todo">
                  <?php
                  require 'db.php';
                  $db = new Db();
                  $query = "SELECT*FROM list";
                  $results = $mysql->query($query);

                  if($results->num_rows){
                      while($row = $results->fetch_object()) {
                          $title = $row->title;
                          $description = $row->description;
                          $id = $row->id;

                          //the rest of the code go here
                 
                  }
                  ?>
</div>

could anyone figure it out for me?i really desperate right now as i could not continue my work because of this just small error.

Thanks in advance.

Member Avatar
aquilax
Junior Poster in Training
71 posts since Jul 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

Test with this code:

<?php
//my database class
class Db {

    // make sure we dont connnect database to every function
    public $mysql;

    function __contruct(){
        $this->mysql = new mysqli('localhost','root','password','db');
        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}


$db = new DB();
var_dump($db->mysql);
?>
Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Test with this code:

<?php
//my database class
class Db {

    // make sure we dont connnect database to every function
    public $mysql;

    function __contruct(){
        $this->mysql = new mysqli('localhost','root','password','db');
        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}


$db = new DB();
var_dump($db->mysql);
?>

hurm...not working also..what change is only the error message which is this:


NULL
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\listnote\index.php on line 25


is this somehow something wrong with my php version?
i am running php version 5.3.0..

Member Avatar
aquilax
Junior Poster in Training
71 posts since Jul 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

Yup:

The mysqli extension allows you to access the functionality provided by MySQL 4.1 and above.

http://www.php.net/manual/en/intro.mysqli.php

I think you can use the mysql extension in php 3.0 :

http://www.php.net/manual/en/book.mysql.php

Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Test with this code:

<?php
//my database class
class Db {

    // make sure we dont connnect database to every function
    public $mysql;

    function __contruct(){
        $this->mysql = new mysqli('localhost','root','password','db');
        if (mysqli_connect_error()) {
            die('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
    }
}


$db = new DB();
var_dump($db->mysql);
?>

hurm...not working also..what change is only the error message which is this:


NULL
Fatal error: Call to a member function query() on a non-object in C:\xampp\htdocs\listnote\index.php on line 25


is this somehow something wrong with my php version?
i am running php version 5.3.0..

Member Avatar
aquilax
Junior Poster in Training
71 posts since Jul 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

Check if you have the mysqli module installed (with phpinfo()) also check if you can actually connect to mysql with the given user/pass/database.

Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Yup:

http://www.php.net/manual/en/intro.mysqli.php

I think you can use the mysql extension in php 3.0 :

http://www.php.net/manual/en/book.mysql.php

em but somehow it is not about my php version or mysql version..
i am running php 5.3.0 and my mysql 5.1.37.

i am pretty sure that this code that i wrote is working just fine by using the php and mysql version lower than i have now.

but if this is not the problem..what else?please help..i really confuse and frustrated right now..

Member Avatar
aquilax
Junior Poster in Training
71 posts since Jul 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

Check my last comment.

Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Check my last comment.

okies i run the phpinfo() but i dont know where to look

but here is some of the things from my phpinfo() :

MysqlI Support enabled
Client API library version 5.1.37
Active Persistent Links 0
Inactive Persistent Links 0
Active Links 36
Client API header version 5.1.37
MYSQLI_SOCKET MySQL

Directive-----------------------Local Value------Master Value
mysqli.allow_local_infile---------On On
mysqli.allow_persistent-----------On On
mysqli.default_host------ no value no value
mysqli.default_port 3306 3306
mysqli.default_pw no value no value
mysqli.default_socket MySQL MySQL
mysqli.default_user no value no value
mysqli.max_links Unlimited Unlimited
mysqli.max_persistent Unlimited Unlimited
mysqli.reconnect Off Off

is there something wrong?

Member Avatar
aquilax
Junior Poster in Training
71 posts since Jul 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

No you have the extension. Did you try to login to the mysql server ?

Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

ya i login to mysql server using the same user and password i wrote for the connection to the db..and everything is okay..

hurm..

Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

could anyone try my code onto your computer and find out if the u got the same error?
i wonder if it is just me that having this problem..

Member Avatar
aquilax
Junior Poster in Training
71 posts since Jul 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

Got it :)

__contrcuct() should be __construct()

Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

oh wait actually my code for db.php is like this :

<?php
                  require 'db.php';
                  $db = new Db();
                  $query = "SELECT*FROM list ORDER BY id asc";
                  $results = $db->mysql->query($query);

                  if($results->num_rows){
                      while($row = $results->fetch_object()) {
                          $title = $row->title;
                          $description = $row->description;
                          $id = $row->id;
                         
                          //the rest of the code go here...
                      }
                  }
?>
Member Avatar
samsons17
Posting Whiz in Training
246 posts since Oct 2009
Reputation Points: -4 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Got it :)

__contrcuct() should be __construct()

hey........thank youuuu so much :D
i didnt realize that one is the problem.. >.<

hoho..must be really careful next time..
thanks again for ur time..

Question Answered as of 2 Years Ago by aquilax
Member Avatar
aquilax
Junior Poster in Training
71 posts since Jul 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

The error is in the db.php class:

<?php
//my database class
class Db {

    // make sure we dont connnect database to every function
    public $mysql;

    function __construct(){
        $this->mysql = new mysqli('localhost','root','password','db') or die ("problem");
    }
?>
Member Avatar
aquilax
Junior Poster in Training
71 posts since Jul 2010
Reputation Points: 7 [?]
Q&As Helped to Solve: 11 [?]
Skill Endorsements: 0 [?]
 
0
 

Glad I could help

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article