I defined $conn in a file and now I am trying to use it within a function in another file. I used the 'global' keyword but it still gives me errors. This is the code
<?php
function redirect_to($new_location){
header("Location: " . $new_location);
exit;
}
function query($query){
global $conn;
return mysqli_query($conn,$query);
}
function confirm($result){
global $conn;
if(!$result){
die("Query Failed ". mysqli_error($conn));
}
}
function fetch_array($result){
return mysqli_fetch_array($result);
}
function getbooks(){
$sql = query("SELECT * FROM books ORDER BY Id DESC");
confirm($sql);
while($row = fetch_array($sql)){
echo $row['Author'];
}
}
?>
this is the file where I defined conn
<?php
$conn = mysqli_connect("localhost", "root", "azonto47","Libraria");
if (!$conn){
die("Connection failed: ".mysqli_connect_error());
}
?>
您必须包含定义$conn
的文件。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.