I am new to sqli ,just started learning oops . I have this code in my db.php
$db = new mysqli('localhost', 'root', '', 'mydb');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
Another file func.php
<?php
include_once 'db.php';
function get_users()
{
$sql="SELECT * from users";
$result=$db->query($sql);
while($row = $result->fetch_assoc())
{
$data[]=$row;
}
return $data;
}
var_dump(get_users());
Error : Undefined variable: db
and when i use
global $db;
inside function
Error : Call to a member function query() on a non-object in
What is the problem here and how can i correct it ?