Echoing an array item


#1

I have a loop which loops through various rota (staff lists) for each day day of the week as part of that loop I have the following code that I want to use to pull the filename of an image out of my database to display a users photo.

The function:

// FETCH MAIN PHOTO FOR USER
function mainUserPhoto_byname($user_name){
    require 'includes/db.config.php';    
    $query = "SELECT  
                mainphoto_file
            FROM media_mainphotos WHERE user_name = '$user_name'
        "; 
    try 
    { 
        $stmt = $conn->prepare($query); 
        $stmt->execute(); 
    } 
    catch(PDOException $ex) 
    { 
        die("Failed to run query: " . $ex->getMessage()); 
    } 
    $display_main_photo = $stmt->fetchAll(PDO::FETCH_ASSOC);
    return $display_main_photo;
}

The line of code I want to populate with the filename value (note I have left the echo as var_dump but obviously want to replace that with just the filename portion on the array:

<?php $main_photo = mainUserPhoto_byname($rota_sunday['user_name']); ?>
<p style="font-size: 13px !important;">[img]<?php echo var_dump($main_photo); ?>[/img]</p>

The var_dump shows the correct values do exist in the array:

array(1) { [0]=> array(1) { ["mainphoto_file"]=> string(61) "30132-nina-04.jpg" } }

No matter what I try I can not get the filename to echo properly - I either get an undefined index error or simply the word Array.


#2

I’m way too rusty at PHP but wonder if it can interpolate that variable in a single quoted string. Seems it used to only work with double quotes.

    $query = 'SELECT  
                mainphoto_file
            FROM media_mainphotos WHERE user_name = "$user_name"
        '; 

which looks way too wonky. How about,

    $query = "SELECT  
                mainphoto_file
            FROM media_mainphotos WHERE user_name = $user_name
        "; 

with no quotes around the variable, or with escaped quotes?

    $query = "SELECT  
                mainphoto_file
            FROM media_mainphotos WHERE user_name = \"$user_name\"
        "; 

#3

Thanks but that hasn’t solved my issue, I’m still only having either the error or Array returned.