PHP : how to pass resultset one page to another using function

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

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...