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.

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);
?>

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..

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..

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.

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..

Check my last comment.

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?

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

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..

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..

Got it :)

__contrcuct() should be __construct()

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...
                      }
                  }
?>

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..

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");
    }
?>

Glad I could help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.