3

I have a php class that interacts with mysql database and then fetches an array as a result.

$res = $db->getResult(); // $res is an array 

I use it with print_r :

print_r($res); 

and it outputs this:

 Array ( [0] => Array ( [id] => 1 [firstname] => Mohamed [lastname] => Kadri ) [1] => Array ( [id] => 2 [firstname] => Slim [lastname] => Nejmaoui ) [2] => Array ( [id] => 3 [firstname] => Sameh [lastname] => Chraiti ) 

And with foreach:

foreach ($res as $row) { echo $row['id'] . ' ' . $row['firstname'] . ' ' . $row['lastname'] . '<br />'; } 

it outputs this:

1 Mohamed Kadri 2 Slim Nejmaoui 3 Sameh Chraiti 

but when there is only one row, it shows this:

1 1 1 M M M K K K 

(1 is the id, M is the first letter in the firstname and K is the first letter in the lastname).

So maybe when there is more than one row the class generates a multidimensional array and the foreach will work on it, and when there is only one row it generates a simple array that will be treated as a multidimensional array by this exact foreach.

So should I make a condition to process one of two types of foreach?

Thanks.

1
  • Enable the reporting of all types of errors, so PHP tells you about potential issues in your code. Commented Jun 22, 2011 at 16:08

4 Answers 4

3

Yep, it looks like that's exactly what's happening. Most likely, $db->getResult() returns the resulting row as an array (not sub-arrays within the result) when there is only one row returned from the query. So, you'll need to detect if there are nested arrays and handle appropriately:

if (isset($res[0]) && is_array($res[0])){ foreach ($res as $row) { echo $row['id'] . ' ' . $row['firstname'] . ' ' . $row['lastname'] . '<br />'; } }else{ echo $res['id'] . ' ' . $res['firstname'] . ' ' . $res['lastname'] . '<br />'; } 
Sign up to request clarification or add additional context in comments.

3 Comments

here it works but it returns an error: Notice: Undefined offset: 0 in D:\www\test\index.php on line 51 (which is the first line of your code "if ($res[0]){")
you should better to test if current($res) is an array or a string, the error is because in case of single result you don't have a $res[0] setted, you may also just change the if($res[0]) by if(isset($res[0])).
You're both correct - that is just my shorthand to test if variables are there, but it's not technically correct. Answer edited.
2

apparently your db object return a single array when you have only one result somthing like that:

Array ( [id] => 2 [firstname] => Slim [lastname] => Nejmaoui ) 

you can test first input and if this is not an array don't loop thrue the foreach else do a foreach

if( is_array(current($res)) ){ // do the foreach... }else{ echo $res['id'] . ' ' . $res['firstname'] . ' ' . $res['lastname'] . '<br />'; } 

I think you should change your db class to always return a nested array even on single row result to keep your results consistents.

1 Comment

ok here it works well, but if I have a zero results array, it gets a error telling that these indexes (id, firstname, lastname) are undefined. so how to fix that?
1

Are you using CodeIgniter? Because it looks like you are. In CodeIgniter, when there is only one result, it only returns one array.

My suggestion? Create a support function (do_something_with_row below) and then call it conditionally:

if(count($res) > 1) { foreach($res as $row) { do_something_with_a_row($row); } } else { do_something_with_a_row($res); } 

1 Comment

How so? What are you getting and what do you expect?
1

You are iterating over one result-row in the case that is unclear to you.

What happens unnoticed to you is that $row then contains only scalar values:

  1. 1 (int)
  2. Mohamed (string)
  3. KadriK (string)

This is the contents for $row already per each iteration (1-3).

These values are getting accessed with the substring access syntax that looks exactly like an array, making use of square brackets.

As all indezies 'id', 'firstname' and 'firstname' are interpreted as constants which are all undefined and result in 0, you're actually accessing the first character/byte in the string: 1, M and K.

With a newer version of PHP the 1 would not even be returned.

Now the important part: If you develop or you look for errors, enable the logging of notices. They warn you about such things:

PHP Notice: Use of undefined constant Kadri - assumed 'Kadri' in ... 

A quick setup can be done by adding this:

 error_reporting(-1); ini_set('display_errors', 1); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.