In a normal PHP file, included above the files that include it, with the $conn
variable globalized: global $conn;
, PHP returns this error.
Notice: Undefined variable: conn in C:_server\htdocs\sites_\tfs\core\php\populate.php on line 5
Fatal error: Uncaught Error: Call to a member function query() on null in C:_server\htdocs\sites_\tfs\core\php\populate.php:5 Stack trace: #0 C:_server\htdocs\sites_\tfs\admin\index.php(59): tableEnlisted() #1 {main} thrown in C:_server\htdocs\sites_\tfs\core\php\populate.php on line 5
db.php
<?php
global $conn;
$u = 'username';
$p = 'password';
$h = 'example.net';
$db = 'example';
// Create connection
$conn = new mysqli($h, $u, $p, $db);
// Check connection
if ($conn->connect_error) {
msg('error',"There was a fatal error connecting to the database: ". $conn->connect_error, 'Fatal Database Error');
}
populate.php (Currently the only core file requesting $conn
)
<?php
function tableEnlisted() {
$sql = "SELECT A_I, qid, toy, toy_varient, date FROM tfs_enlisted ORDER BY A_I DESC LIMIT 500";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$sql = "SELECT last_name, firstname, class FROM tfs_students WHERE qid LIKE `".$row['qid']."`";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$last = $row['last_name'];
$first = $row['last_name'];
$class = $row['class'];
echo
'<tr>
<td>'.$row['A_I'].'</td>
<td>'.$last.'</td>
<td>'.$first.'</td>
<td>'.$class.'</td>
<td>'.$row['toy'].'</td>
<td>'.$row['toy_varient'].'</td>
<td>'.$row['date'].'</td>
<td>
<div class="btn-group">
<button class="btn btn-secondary btn-sm dropdown-toggle" type="button" data-toggle="dropdown" aria-haspopup="false" aria-expanded="false">
Options
</button>
<div class="dropdown-menu">
<a href="view?mode=edit&row='.$row['A_I'].'"><span class="text-primary">Edit</span></a>
<a href="view?mode=view&row='.$row['A_I'].'"><span class="text-primary">View</span></a>
<a href="view?mode=void&row='.$row['A_I'].'"><span class="text-danger">Void</span></a>
</div>
</div>
</td>
</tr>';
}
}
else {
msg('warn','User with the ID '.$row['qid'].' does not exist!','Unknown User');
}
}
}
else {
msg('info','The table returned empty.','Empty!');
}
}
include_once
order
<?php
include_once('../core/php/msgs.php');
include_once('../core/php/db.php');
include_once('../core/php/oauth.php');
include_once('../core/php/populate.php');
?>