this is probebly a silly question but I don't know where else to ask this. I need to call on $conn in the functions below, but if I try to turn it into a global it doesn't work. If someone can explain to me how to use an outside variable inside a function I'd be very grateful.
Here's my little piece of code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//Maak connectie met database
$host = "localhost";
$username = "i266161_studie";
$password = "studie";
$dbnaam = "test";
$fout = "Error: Failed to open database";
$conn = mysqli_connect($host, $username, $password, $dbnaam)
or die ("Connection failed: ".mysqli_connect_error());
function insert_once($conn){
$query = 'INSERT INTO gebruikers (naam, leeftijd, woonplaats)
VALUES ("Henk", 17, "Eindhoven")';
mysqli_query($conn,$query);
}
function insert_twice($conn){
$query = 'INSERT INTO gebruikers (naam, leeftijd, woonplaats)
VALUES ("Bert", 21, "Helmond"), ("Ruud", 32, "Helmond")';
mysqli_query($conn,$query);
}
insert_twice();
?>