6 Replies - 1842 Views - Last Post: 15 March 2013 - 12:38 PM Rate Topic: -----

#1 aldherl  Icon User is offline

  • New D.I.C Head

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

Call to undefined method dbConnection::query() - How to fix it?

Posted 14 March 2013 - 01:38 AM

Hi guys!

I am new here. I am having problems with my site that I am developing. Could you help me how to fix it.

This is the complete error statement, Fatal error: Call to undefined method dbConnection::query() in C:\xampp\htdocs\koa\classes\class.ManageUsers.php on line 20.
I am using PHP, and MySQL in an Object Oriented way.

Here is the code in the class.ManageUsers.php that the error is pointing to. I will put the whole function here:.
function LoginUsers($username,$password){
			$query = $this->db->query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");
			$num_rows = $this->link->fetchRows();
			return $num_rows;
		}


The line 20 is:
	$query = $this->db->query("SELECT * FROM users WHERE username = '$username' AND password = '$password'");



Also the construct function here:
		function __construct(){
			$this->db = new dbConnection();
			$this->db->connect();
		}


The dbConnection class is this:
class dbConnection{
		protected $db_conn, $_query, $_numRows, $_fetchAll;
		public $db_name = '******';
		public $db_user = '******';
		public $db_pass = '******';
		public $db_host = '******';
		
		function connect(){	
			$this->db_conn = mysql_connect($db_host, $db_user, $db_pass);
			mysql_select_db($db_name, $this->db_conn);
			if(mysql_errno($this->db_conn){
				return mysql_error($this->db_conn);
			}
			else{
				return $this->db_conn;
			}
		}
		
		public function query($sql){
			$this->_query = mysql_query($sql, $this->db_conn);
			$this->_numRows = mysql_num_rows($this->_query);
			$this->_fetchAll = mysql_fetch_array($this->_query);
		}
}
?>


Thanks in advance! :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: Call to undefined method dbConnection::query() - How to fix it?

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Call to undefined method dbConnection::query() - How to fix it?

Posted 14 March 2013 - 01:52 AM

Hey.

There is a syntax error on line #11 in your dbConnection class. You're missing an end parenthesis.

By the way, the old MySQL functions (mysql_connect, mysql_query, etc...) are outdated and shouldn't be used. Even wrapped in an OOP layer. Look into PDO or MySQLi instead. Those are already OOP :)
Was This Post Helpful? 1
  • +
  • -

#3 aldherl  Icon User is offline

  • New D.I.C Head

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

Re: Call to undefined method dbConnection::query() - How to fix it?

Posted 14 March 2013 - 02:10 AM

Yeah I used both PDO and MySQLi in my problem but that is still the problem. I used PDO first when I am only using in the locahost. But, when I put it in to a free host, there are a lot of errors. So, I decided to put it in MySQL way. :)/>

This post has been edited by Dormilich: 14 March 2013 - 03:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,829
  • Joined: 30-April 10

Re: Call to undefined method dbConnection::query() - How to fix it?

Posted 14 March 2013 - 08:03 PM

Why not just use one method PDO or MySQLi?

This post has been edited by Dormilich: 16 March 2013 - 03:59 AM

Was This Post Helpful? 0
  • +
  • -

#5 aldherl  Icon User is offline

  • New D.I.C Head

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

Re: Call to undefined method dbConnection::query() - How to fix it?

Posted 14 March 2013 - 09:27 PM

If I used either of the two. I'm getting this error:

Quote

Fatal error: Call to a member function query() on a non-object in /srv/disk4/1326121/www/koasamp1.atwebpages.com/todo/classes/class.ManageUsers.php on line 21

This post has been edited by Dormilich: 16 March 2013 - 03:59 AM

Was This Post Helpful? 0
  • +
  • -

#6 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,829
  • Joined: 30-April 10

Re: Call to undefined method dbConnection::query() - How to fix it?

Posted 15 March 2013 - 10:38 AM

You have to make sure you are connecting with the one you choose.

MySQLi->
OOP connection:
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");


Procedural:
$link = mysqli_connect("localhost", "my_user", "my_password", "world");


PDO->
OOP connection:
$conn = new PDO("mysql:host=localhost;dbname=database;","root","");

Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,436
  • Joined: 23-August 08

Re: Call to undefined method dbConnection::query() - How to fix it?

Posted 15 March 2013 - 12:38 PM

This code demonstrates what you're doing wrong, and how to do it right. See if you can't figure it out:

<?php

class MyDb
{
	public $db_host = "mydbhost";

	public function connect()
	{
		print("DB HOST: {$db_host}\n");
	}
}

class MyGoodDb
{
	public $db_host = "mydbhost";

	public function connect()
	{
		print("DB HOST: {$this->db_host}\n");
	}
}

$myDb = new MyDb();
$myDb->connect();

$myGoodDb = new MyGoodDb();
$myGoodDb->connect();

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1