I am working on a PHP application in which I am trying mysqli instead of MySQL. Following is the code in file db_functions.php
:
function get_database_connection(){
$conn = new mysqli('localhost', 'user', 'password', 'dbname');
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
return $conn;
}
function execute_sql_query($query, $error_message){
$result = "";
$query = trim($query);
$conn = get_database_connection();
$result = $conn->query($query);
$conn->close();
var_dump($result);
return $result;
}
In another file index.php, I am including db_functions.php and calling execute_sql_query method. Following is the code:
$sql = "select * from employee";
$result = execute_sql_query($sql, "");
$tip_array = array();
while($row = $result->fetch_assoc()){
$uname = $row['uname'];
}
When I run this, it shows error:
Call to a member function fetch_assoc() on a non-object