Hi,
I been learning OOP slowly. I'm stuck how to resolved an function query() on a non object issue.
My files are very basic:
This is my db.php file:
<?php
global $db;
class foo_mysqli extends mysqli {
public function __construct($host, $user, $pass, $db) {
parent::__construct($host, $user, $pass, $db);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
}
}
$db = new foo_mysqli('localhost', 'my_user', 'my_password', 'my_db');
$db->close();
?>
This is my function.php file
<?php
include("db.php");
function PM() {
$sql = "SELECT * FROM posts";
$result = $db->query($sql) or die("It's not connecting");
while($row = $result->fetch_assoc($sql)) {
echo $row['Content'];
}
}
?>
This is my index.php file (This is where my data is echo out):
<?php
include('include/function.php');
PM();
?>
The error I'm having is in the function.php on line 6.
Which is this line:
$result = $db->query($sql) or die("It's not connecting");
I try to used this format and it didn't work well either it kept saying (mysqli_query() expects parameter 1 to be mysqli):
<?php
include("db.php");
function PM() {
$query = "SELECT * FROM posts";
$result = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
while($row = mysqli_fetch_assoc($query)) {
echo $row['Content'];
}
}
?>
and this
<?php
include("db.php");
function PM() {
$sql = $db->query("SELECT * FROM posts") or die("Problem with query");
while($row = mysqli_fetch_assoc($sql)) {
echo $row['Content'];
}
}
?>
Any Suggestions and explanation will help. I appreciate it. Thanks!