27335

Why do I get 'Undefined offset: 0'?

Question:

I'm develop a website and it looks fine. Then I turned on debugging to see if I had any errors, and I get this message:

Notice: Undefined offset: 0 in I:\path\to\file\MyFile.class.php on line 105

On that line you will find the following code:

$result = $stmt->fetchAll(PDO::FETCH_ASSOC); return $result[0]; // line 105

If I do a print_r(result) I get the following result:

Array ( [0] => Array ( [id] => 3721 [parentID] => ) ) Array ( )

As you can see the result contains twoarrays. The first one has data, the second one has no data. I've never seen print_r output two arrays like this. I'm guessing it's the second one causing me the problems.

Can anyone advice me of why that second array sneaks in?

<strong>VAR dump</strong>

array 0 => array 'id' => string '3721' (length=4) 'parentID' => null array empty

Answer1:

You fetch all records matching the query. If there are <em>no</em> records it will return an empty array. Obviously there is no 0 element in an empty array.

Recommend

  • PDO throwing error if MYSQL_ATTR_INIT_COMMAND is used
  • Foreach loop using string to output XML
  • Getting the id of the last inserted record from an MSSQL table using PDO and PHP
  • PHP code displaying as comments in browser using WAMP [closed]
  • Something wrong with Singleton class, no idea what
  • Zend\\ServiceManager\\ServiceManager::get was unable to fetch or create an instance for getAlbum
  • MySQLi using an IN in a where
  • have mysql select statement return fully qualified column names like table.field
  • When to use PDO prepared queries. mysql_real_escape error
  • Using Select and where statement in Criteria
  • Arrange forum posts by time of latest reply
  • HSQLDB Statement and Java NaN doubles
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • Perl system calls when running as another user using sudo
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • php design question - will a Helper help here?
  • A cron job substitute?
  • json Serialization in asp
  • AngularJs get employee from factory
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Java static initializers and reflection
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • unknown Exception android
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?