I have a function named record_list()
which helps me echo fetched query from database every time I refresh/visit a page. I tried to use this function by echoing it twice but unfortunately all my DOM elements after the script get hidden and I can't use the following divisions/lists in my HTML. But if I call it once it's working fine without any problem.
Apart form this I am getting this error:
Fatal error: Call to a member function query() on a non-object
record_list():
function record_list(){
//include db configuration file
include_once("configuration.php");
try{
//MySQL query
$user_id = $_SESSION['user_id'];
$results = $mysqli->query("SELECT * FROM education_record WHERE user_id='$user_id' ");
//get all records from add_delete_record table
while($row = $results->fetch_assoc())
{
echo '<li class = "col-md-4 col-xs-12 records_display" id="item_'.$row["id"].'">';
echo '<div class="del_wrapper"><a href="#" class="del_btn" id="del-'.$row["id"].'">';
echo '<img src="../img/removeButtonIcon.svg" height ="20px" width ="20px" border="0" />';
echo '</a></div>';
echo '<div class="controls group_row">';
echo '<div class="controls group">';
echo '<input disabled type="text"class="group"style="width:175px" value ="'.$row["degree_name"].'"/>';
echo '<input disabled type="text"class="group"style="width:175px" value ="'.$row["institute"].'"/>';
echo '<input disabled type="text"class="group"style="width:175px" value ="'.$row["specialisation"].'"/>';
echo '<input disabled type="text"class="group"style="width:100px" value ="'.$row["date_of_passing"].'"/>';
echo '</div>';
echo '</div>';
echo '</li>';
}
}
catch (mysqli_sql_exception $e) {
throw $e;
die();
}
$mysqli->close();
//close db connection
}
configuration.php:
<?php
$host = 'localhost';
$dbname = 'databasename';
$username = 'username';
$password = 'can-be-anything';
try {
$mysqli = new mysqli($host, $username, $password, $dbname);
} catch (mysqli_sql_exception $e) {
throw $e;
die();
}
?>
Please help me identifying this error.
See Question&Answers more detail:os