I have a script where I am calling a function to query a database and pull the filename that is stored there.
In the code below it is the last block that is giving me problems.
The echo command sends the correct file name to the screen. But when I leave the function and try to use the variable $img_name I get an error message that this is an undefined variable:
Encountered error: 8 in /home/omgma/public_html/member_community/profile/profile_updt_form.php, line 181: Undefined variable: img_name
function get_img_filename($username)
{
$conn = db_connect();
$result = $conn->query("select user_img_filename from users where username = '$username'");
$row = $result->fetch_object();
$img_name = $row->user_img_filename;
echo "This is image name: $img_name";
return $img_name;
}
The script where I call the function is as follows:
<?php
//show current photo if exists
//This will be a query of the database to see if there is an existing image name stored in db
$imgs_path = '/home/omgma/public_html/member_community/profile/profile_imgs/' ;
//if there is then show the existing photo
//check for the $newfilename variable
if (!empty($newfilename))
{
//$newfilename is not empty so use name in processing
$imgs_path .= $newfilename ;
}
else
{//$newfilename is empty so find the filename in db
$username = $_SESSION['valid_user'];
get_img_filename($username);
}
//check to see if a user photo exists
$imgs_path .= $img_name ;
if (file_exists($imgs_path) )
{
print "<div><img src='profile_imgs/".$img_name ."' alt='".$username."' /></div>";
print "<p>Path and nameVariables: </p>";
var_dump ($imgs_path, $img_name);
}
else
{
print "<div><img src='profile_imgs/no_photo.jpg' alt='No Image Available' /></div>";
/*
print "<p>No image could be found - path and name variables:</p>";
var_dump ($imgs_path, $newfilename);
*/
}
?>
So how do I make the img_name variable that is set to the query results in the function available to the script that calls the function?