Pass mysql resultset from one to another using function of php, you need to create a function to create the mysql query and return the result set.
STEP 1. create db.php
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
STEP 2. create dbmodel.php
<?php
require_once('db.php');
function process($conn){
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
return $result;
}
?>
STEP 3. create resultdetails.php
<?php
require_once('dbmodel.php');
$result=process($conn);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
?>
No comments:
Post a Comment