What am I doing wrong with this "foreach...?"

When I run this:

$statement = $mssql_pdo->prepare("select * from gant_table");
$statement->execute();
$results=$statement_row=$statement->fetch(PDO::FETCH_ASSOC);

foreach($results as $row) {
   var_dump($row);
}

I get this:

string(1) "1" string(13) "str_ProjectId" string(1) "0" string(1) "0" string(1) "0" string(15) "str_ProjectName" string(1) "0" string(15) "str_Description" string(1) "0" string(1) "0" string(1) "0" string(17) "str_DateSubmitted" string(10) "0000-00-00" string(1) "0" string(1) "0" string(1) "0" string(1) "0" string(1) "0" string(17) "str_CurrentStatus" string(20) "str_RequestorDueDate" string(15) "str_DateStarted" string(31) "str_DocumentationCompletionDate" string(24) "str_TargetCompletionDate" string(17) "str_DateCompleted" string(20) "str_DateOnProduction" string(10) "0000-00-00" string(10) "0000-00-00" string(10) "0000-00-00" string(10) "0000-00-00" string(10) "0000-00-00" string(9) "str_Notes" string(1) "0" string(1) "0" string(10) "0000-00-00" string(1) "0" string(10) "0000-00-00" string(8) "str_Tool" string(19) "str_ProjectPriority" string(15) "str_ProjectType" string(23) "str_DocumentationStatus" string(17) "str_ProjectStatus" string(17) "str_LevelOfEffort" string(23) "str_Full_Name_Developer" string(23) "str_Full_Name_Submitter" string(24) "str_Full_Name_InsertUser" string(24) "str_Full_Name_UpdateUser" string(19) "str_Full_Name_Owner" string(10) "0000-00-00" string(10) "0000-00-00" string(1) "0" string(1) "0" string(1) "0" string(1) "0" string(1) "0" string(1) "0" 

Open in new window


When I try to break it down like this:

foreach($results as $row) {
   echo $row['str_ProjectName'].'<br>';
}

I get this:

Warning: Illegal string offset 'str_ProjectName' in C:\wamp\www\SouthArea\GANT\gant.php on line 45
 1

Warning: Illegal string offset 'str_ProjectName' in C:\wamp\www\SouthArea\GANT\gant.php on line 45
 s

Warning: Illegal string offset 'str_ProjectName' in C:\wamp\www\SouthArea\GANT\gant.php on line 45
 0

Warning: Illegal string offset 'str_ProjectName' in C:\wamp\www\SouthArea\GANT\gant.php on line 45
 0

Warning: Illegal string offset 'str_ProjectName' in C:\wamp\www\SouthArea\GANT\gant.php on line 45

Open in new window


It's repeated 127 times which matches the number of rows in the database, but I'm expecting the Project Name and instead I'm getting an "illegal offset."

What am I missing? What am I doing wrong? I should be getting things like "Switch Variance Report Enhancement" and instead I get that nonsense.

What do you think?
brucegustPHP DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
$results=$statement_row=$statement->fetch(PDO::FETCH_ASSOC);

Open in new window

Why this double assignement?
$results=$statement->fetch(PDO::FETCH_ASSOC);

Open in new window

should be enough and give expected result...
0
brucegustPHP DeveloperAuthor Commented:
Did this:

$statement = $mssql_pdo->prepare("select * from gant_table");
$statement->execute();
$results=$statement->fetch(PDO::FETCH_ASSOC);

foreach($results as $row) {
   echo $row['str_ProjectName'].'<br>';
}

...and got the same error.

What else could it be?
0
Marco GasiFreelancerCommented:
It looks like your result is a string not an array: in fact, usually var_dump output starts with
array[number_of_elements](

)

Open in new window

So the problem is: whay do you get a string? Forgive me if I have to go on by try and error, but I would try to use fetchAll:

$results=$statement->fetchAll(PDO::FETCH_ASSOC);

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

Marco GasiFreelancerCommented:
It should be right: this is the definition for fetch() method:
"Fetches a row from a result set associated with a PDOStatement object"
and this is the one for fetchAll()
"Returns an array containing all of the result set rows"

See here: http://php.net/manual/en/pdostatement.fetch.php
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
brucegustPHP DeveloperAuthor Commented:
That worked!

So, in other words, I was using the wrong kind of methodology to retrieve what amounted to several rows worth of info.

Whereas "fetch" is retrieving one row, "fetchall" is retrieving what is expected to be a number of rows worth of info.

Both approaches will yield an array, yes?

You have the points, but, if you're willing, I would love to get a better handle on "why" your solution worked and not just "that" it worked.

Thanks!
0
Marco GasiFreelancerCommented:
Hi brucegust. Thanks for points. Now I have to go, but later I'll be happy to clarify. Cheers
0
Marco GasiFreelancerCommented:
I'll try to explain even if my english is not perfect :-)
fetch() method gets just one row, so the array holds all the columns in one row. Then, you can write
echo $results['str_ProjectName'];

Open in new window

and get the expected output. You can also write
for($i=0;$i<count($results);$i++)
{
	echo $results['str_ProjectName']."<br>";
}

Open in new window

and this too will give the expected output.
But if you use foreach loop
foreach ($results as $row)
{
	echo $row['str_ProjectName']."<br>";
}

Open in new window

you are using each item of the array (which is a database column values) as it were an array whereas it's just a string - a database column's value, letter by letter. It's obvious that 'str_ProjectName' is not a valid index iterating through the word 'MyProject'.

In other words, the array returned by fetch() method is like this:
array(
  "name" => "value",
  "name" => "value"
);

Open in new window

The array returned by fetchAll() is instead like this one:
array(
    [0] => array(
                    "name" => "value",
                    "name" => "value"
                ),
    [1] => array(
                    "name" => "value",
                    "name" => "value"
                )
);

Open in new window

Hope this is clear enough :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.