Working on a small project with some simple sql query injection in my php file. I have created a functions.php file with a function called function displayimage()
. I include my function file in my index file and use the function like so
index.php
<div class="col-lg-2">
<?php displayimage(); ?>
</div>
Functions.php
function displayimage()
{
$dbCon = mysqli_connect("localhost", "root", "root", "testdb");
if (mysqli_connect_errno()) {
echo "Failed to connect: " . mysqli_connect_error();
}
$sql= "SELECT * FROM `images` ORDER BY `images`.`id` DESC ";
$query=mysqli_query($dbCon, $sql);
if ($row = mysqli_fetch_array($query))
{
echo '<img class="img-responsive" style="margin-top: 10px;" src="data:image;base64,'.$row[2].' "> ';
}
mysqli_close($dbCon);
}
?>
So it works fine but.. I tried to clean my code by putting the database connection in a seperate file, and including it like include('connection.php');
. Unfortunately my code doesn't work anymore, and the content won't show up at my index file. My PHPStorm says that $dbCon
is a undefinable variable now. What am I doing wrong here?
new functions.php
function displayimage()
{
include('connection.php');
$sql= "SELECT * FROM `images` ORDER BY `images`.`id` DESC ";
$query=mysqli_query($dbCon, $sql);
if ($row = mysqli_fetch_array($query))
{
echo '<img class="img-responsive" style="margin-top: 10px;" src="data:image;base64,'.$row[2].' "> ';
}
mysqli_close($dbCon);
}
?>
connection.php
$dbCon = mysqli_connect("localhost", "root", "root", "testdb");
if (mysqli_connect_errno()) {
echo "Failed to connect: " . mysqli_connect_error();
}