0

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

3
Contributors
8
Replies
13
Views
5 Years
Discussion Span
Last Post by extjac
0

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.

0

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();
			}
	}
}
0

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) )
0
global $mysqli;

You don't need this. As I mentioned:

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

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

0
$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()) {

Edited by diafol: n/a

0

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();
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.