Query output with null value

I am trying to flatten a file list where the ARTICLE_NO field is a specific number. At the moment I have a file table with ARTICLE_NO, FILENAME and USERNAME columns. There is one entry with the values ​​1, x.txt and user respectively. $ pk is assigned 1 in my php code. However, the following code only generates NULLs and I'm not sure why.

$filesQuery = "SELECT FILENAME FROM FILES WHERE ARTICLE_NO = ?"; 

if ($getFiles = $con->prepare($filesQuery)) {
    $getFiles->bind_param("s", $pk);
    $getFiles->execute();
    $getFiles->bind_result($FILENAME);
    $files = array();

    while ($getFiles->fetch()) {
         $filenames = array(
         'FILENAME' => $FILENAME,
          );
$files[] = $filenames;
        }
}
var_dump($files['FILENAME']);
    foreach ($files as $filenames)
    {
    $filesList = '<p><a href="./files/'. $files['FILENAME'] .'">'. $files['FILENAME'] .'</a>' . "\n"; 
    }

      

0


source to share


1 answer


You are accessing the array that you created incorrectly. When working with 1 row of result from the database, $ files is an array containing 1 other array, which looks like this:

$files = array ( 0 => array ( 'FILENAME' => 'x.txt' ) );

      

So, to access the value in the FILENAME field, you need to use:



var_dump($files[0]['FILENAME']);

      

or

foreach($files as $file_data) { echo $file_data['FILENAME']; } 

      

0


source







All Articles