Hi, i am trying to make this work but i guess i am missing something.....

This is what i have:

"db.php"

<?php
DEFINE('DATABASE_USER', 'root');
DEFINE('DATABASE_PASSWORD', '123456');
DEFINE('DATABASE_HOST', 'localhost');
DEFINE('DATABASE_NAME', 'livesearch');
$mysqli = new mysqli(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD, DATABASE_NAME);
if (mysqli_connect_errno()) {
	printf("Connect failed: %s\n", mysqli_connect_error());
	exit();
}
	else {
	//connected	
}
?>

"test.php"

<?php

class article   { 

	function getArticles() 
	{
		
		$query = "
				SELECT * 
				FROM topics 
				WHERE topictitle like '%$keyword%' or topicdescription like '%$keyword%';
				";
			if ($result = $mysqli->query($query) ) 
			{
				while ($row = $result->fetch_assoc()) {
					echo $row['topictitle'].'<br>';
					echo $row['topicdescription'].'<br><br>';
				}
			$result->close();
			}
	}
}


include 'db.php';
$Article = new article($mysqli);
$Article->getArticles();
?>

The error I am getting is:
"Fatal error: Call to a member function query() on a non-object in C:\AppServ\www\test.php on line 13"

This line is not working:
if ($result = $mysqli->query($query) )

I don't know how to pass the $mysqli into the class and then into the function....... PLEASE HELP

Recommended Answers

All 8 Replies

Member Avatar for diafol

you don't seem to have got a mysqli var:

class article{
   private $mysqli;

   public function __construct($mysqli){
      $this->mysqli = $mysqli; //now you can use the object as $this->mysqli throughout your class
   }
}

include('db.php'); //$mysqli object in here?
$article = new article($mysqli); //pass object

I don't know if that will work. I'm only just getting to grips with OOP myself. I've probably over-egged it as usual.

if i do this, it works....But is it a good way?

class article   { 

	private $mysqli;
	 
	function __construct($mysqli){
		$this->mysqli = $mysqli; //now you can use the object as $this->mysqli throughout your class
	}
	
	function getArticles() 
	{
		global $mysqli;
		
		$query = "
				SELECT * 
				FROM topics 
				WHERE topictitle like '%$keyword%' or topicdescription like '%$keyword%';
				";
			if ($result = $mysqli->query($query) ) 
			{
				while ($row = $result->fetch_assoc()) {
					echo $row['topictitle'].'<br>';
					echo $row['topicdescription'].'<br><br>';
				}
			$result->close();
			}
	}
}

if i do this, it works....But is it a good way?

Bad way and here is why

function getArticles() {
		global $mysqli;
}

You are using global instance not the one in class. you should user class property instead and remove the global thing

if ($result = $this->mysqli->query($query) )
Member Avatar for diafol
global $mysqli;

You don't need this. As I mentioned:

//now you can use the object as $this->mysqli throughout your class

sorry for being soo dummy, but this is what i am doing and still doenst work

<?php

class article   { 

	private $mysqli; 
	
	function __construct($mysqli){
		$this->mysqli = $mysqli; //now you can use the object as $this->mysqli throughout your class
	}
	
	function getArticles () 
	{
		$query = "
				SELECT * 
				FROM topics 
				WHERE topictitle like '%$keyword%' or topicdescription like '%$keyword%';
				";
			if ($result = $this->$mysqli->query($query) ) 
			{
				while ($row = $this->$result->fetch_assoc()) {
					echo $row['topictitle'].'<br>';
					echo $row['topicdescription'].'<br><br>';
				}
			$result->close();
			}
	}
}
/* close connection */

include 'db.php';
$Article = new article($mysqli);
$Article->getArticles();
?>

ERROR:
Fatal error: Cannot access empty property in

Member Avatar for diafol
$result = $this->mysqli->query($query

try that

*I think* you have to put the '$' right at the start and then take it off for everything that follows.

//edit

this too:

while ($row = $this->result->fetch_assoc()) {

Thanks!
This worked!

<?php

class article   { 

	function __construct($mysqli){
		$this->mysqli = $mysqli; //now you can use the object as $this->mysqli throughout your class
	}
	
	function getArticles () 
	{
		$query = "
				SELECT * 
				FROM topics 
				WHERE topictitle like '%$keyword%' or topicdescription like '%$keyword%';
				";
			if ($result = $this->mysqli->query($query) ) 
			{
				while ($row = $result->fetch_assoc()) {
					echo $row['topictitle'].'<br>';
					echo $row['topicdescription'].'<br><br>';
				}
			$result->close();
			}
	}
}
/* close connection */

include 'db.php';
$Article = new article($mysqli);
$Article->getArticles();
?>
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.