PHP How can I write this code into a function?

I would like some help on how can I write the code below inside a function.

This is the second file where I'm calling the function.

require "index.php";

$db = new con_db();
$conn = $db -> connection();
$runSQL = new table();

$sql = "insert into students (ID, Name, Gender, Age) values(1234, 'John', 
    'Male', 19)";
$results = $runSQL -> insert($sql, $conn);

This is the index.php file

function insertSQL($query, $conn){
try{
    $dp = $conn -> prepare($query);
    $dp -> execute();   
    $results = $dp -> fetchAll();
    $dp -> closeCursor();
    return $results;
    }
catch(PDOException $e)
    {   
        $error_out = new report_error();
        $error_out -> http_error("Internal Error:\n\n" . $e -> getMessage());
    }
}


Read more here: https://stackoverflow.com/questions/64949460/php-how-can-i-write-this-code-into-a-function

Content Attribution

This content was originally published by Joe at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: