I want to create an function to fetch data from different tables which have different table. Is it possible?
This is my function code
<?php
// Check connection
function fetch_data($selectsql){
include 'connectdb.php';
$result = $conn->query($selectsql);
if($result->num_rows <= 0 ){
echo "0 results";
}
else{
while ($row = $result->fetch_assoc()){
return $row = array();
}
}
}
?>
and this is for use testing my function
<?php
include 'fetch_data_test.php';
include 'connectdb.php';
$selectsql = "SELECT * FROM user";
$row = fetch_data($selectsql);
echo $name = $row["name"];
echo $lastname = $row["lastname"];
$conn->close();
?>
but it didn't work. Can someone help me? or explain me more about how it can get and array.
Error:
Notice: Undefined index: name in /var/www/html/home/use_fetch.php on line 7 Notice: Undefined index: lastname in /var/www/html/home/use_fetch.php on line 8