0

I have a problem with getting MySQL column into php array variable , for further foreach() cycling.

Here is my code:

$files=array();
$filesFetch = "SELECT FileName FROM articledata";
$rs=mysql_query($filesFetch);
while($rd=mysql_fetch_object($rs))
{
$files[]=$rd->files;
}

if (!$files) {
    die('getting filenames failed');
}
foreach ($files as $key => $id) 
{  //... do the stuff }

I am not getting stopped at the die condition, but my error says:

Notice: Undefined property: stdClass::$files

I assume there is problem with formatting, because this error notice repeats :"rowCountTimes"

Also this error appears for the line.

$files[]=$rd->files;
pb2q
  • 58,613
  • 19
  • 146
  • 147
Slytherin
  • 474
  • 3
  • 17
  • Please, don't use `mysql_*` functions to write new code. They are no longer maintained and the community has begun [deprecation process](http://goo.gl/KJveJ). See the [*red box*](http://goo.gl/GPmFd)? Instead you should learn about [prepared statements](http://goo.gl/vn8zQ) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://goo.gl/3gqF9) will help you. If you pick PDO, [here is good tutorial](http://goo.gl/vFWnC). – tereško Oct 11 '12 at 23:02

4 Answers4

2

Your object has properties based on the column names; you need to use:

$files[]=$rd->fileName;
andrewsi
  • 10,807
  • 132
  • 35
  • 51
0

You are fetching FileName, so you must use that in your object:

$files[]=$rd->FileName;
JvdBerg
  • 21,777
  • 8
  • 38
  • 55
0

It should be

$rd->FileName

instead of $rd->files since your column name is FileName

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
0

The code you wrote certainly doesn't make sense. You are fetching an object which is saved in a variable $rd. Then you are trying to access property files but of course that is undefined since you only fetched the column named FileName.

So use instead: $files[] = $rd->FileName;

Aljana Polanc
  • 950
  • 8
  • 12