PDO function not working

I am trying to write a function that will display the users message in out CRM. I am having some trouble with this:
//load Mysql connector
require_once($_SERVER['DOCUMENT_ROOT'] . '/connections/pdoConnect.php');

function getMessages($pdo,$ID) {
    $messages = "SELECT CONCAT(B.firstName, B.lastName) as senderName, (SELECT CONCAT(firstName,' ', lastName) FROM `leads_userInfo` WHERE `userID`=A.recipientID LIMIT 1) as recipientName, A.`subject`, A.`status`, A.`dateCreated` as `date`
FROM simpleNote as A
INNER JOIN leads_userInfo as B
ON A.senderID=B.userID
WHERE `recipientID`=$ID";
    foreach ($pdo->query($messages) as $message) {

        $row =  "<tr>";
        $row .= "<td>" . $message['recipientName'] . "</td>td>";
        $row .= "<td>" . $message['subject'] . "</td>td>";
        $row .= "<td>" . $message['name'] . "</td>td>";
        $row .= "<td>" . $message['name'] . "</td>td>";
        $row .=  "</tr>";
        return $row;

    }
}

Open in new window


What I want to do is call this function down in the table body in the html.

<?= getMessages($_SESSION['id';); ?>

Open in new window


I need help getting the function working correctly.
austinfxAsked:
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.

Julian HansenCommented:
What is wrong with it -
a) does not do what it is expected - if so describe what is expected
b) Shows an error - if so what is the error

At first glance this does not appear to be correct
foreach ($pdo->query($messages) as $message) {
  $row =  "<tr>"; // THIS SHOULD BE A CONCATENATION
  $row .= "<td>" . $message['recipientName'] . "</td>td>";
  $row .= "<td>" . $message['subject'] . "</td>td>";
  $row .= "<td>" . $message['name'] . "</td>td>";
  $row .= "<td>" . $message['name'] . "</td>td>";
  $row .=  "</tr>";
  return $row; // I AM PRETTY SURE YOU DON'T WANT THIS HERE
}

Open in new window

Your loop is never going to go around more than once - the return statement executes after the first iteration and terminates the loop.

In addition your first $row assignment is not a concatenation - so even if the code was looping the $row variable would be re-initialised each time.

I imagine you probably want something like this
// INITIALISE $row HERE
$row = '';
foreach ($pdo->query($messages) as $message) {
  // ADD TO ROW ON EACH LOOP
  $row .=  "<tr>"; 
  $row .= "<td>" . $message['recipientName'] . "</td>td>";
  $row .= "<td>" . $message['subject'] . "</td>td>";
  $row .= "<td>" . $message['name'] . "</td>td>";
  $row .= "<td>" . $message['name'] . "</td>td>";
  $row .=  "</tr>";
}
return $row; // MOVE OUTSIDE OF LOOP

Open in new window

0
Julian HansenCommented:
One more thing - this might be a typo but you have this line twice
        $row .= "<td>" . $message['name'] . "</td>td>";

Open in new window

And more importantly it is referencing an index 'name' that does not appear to be in the field list of the query.
0
Ray PaseurCommented:
If you're new to PHP and trying to learn the language, this article has pointers to well-vetted learning resources.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

austinfxAuthor Commented:
OK So I changed it up This code is working but I am only getting the one record because I am trying to echo the variable outside the loop.
require_once($_SERVER['DOCUMENT_ROOT'] . '/connections/pdoConnect.php');
$_SESSION['id'];
// connect to MySQL
mysql_select_db($database_medigap, $medigap);

// load the new messages
    $messages = "SELECT CONCAT(B.firstName, B.lastName) as senderName, (SELECT CONCAT(firstName,' ', lastName) FROM `leads_userInfo` WHERE `userID`=A.recipientID LIMIT 1) as recipientName, A.`subject`, A.`status`, A.`dateCreated` as `date`

FROM simpleNote as A
INNER JOIN leads_userInfo as B
ON A.senderID=B.userID
WHERE `recipientID`=312";

 $query =  $pdo->query($messages);
$message='';

foreach ($query as $row) {
    $message = "<tr>";
    $message .= "<td>" . $row['recipientName'] . "</td>";
    $message .= "<td>" . $row['subject'] . "</td>";
    $message .= "<td>" . "New" . "</td>";
    $message .= "</tr>";

}

// down in the page:
<?php echo $message; ?>

Open in new window


How does this need to be written to show each table set?
0
Ray PaseurCommented:
This article shows the way its done.  It looks like you're mixing up MySQL and PDO.  Best to choose one or the other and stick with it!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

This page maps the familiar but obsolete MySQL functions to their closest relatives in the MySQLi and PDO extensions.
http://iconoun.com/mysql_mysqli_pdo_function_map.php
0
Julian HansenCommented:
Refer my earlier comment - your first assignment in the loop will wipe out the string you are trying to build up
$message='';

foreach ($query as $row) {
    $message = "<tr>"; /* <=== HERE: SHOULD BE A CONCATENATION */
    $message .= "<td>" . $row['recipientName'] . "</td>";
    $message .= "<td>" . $row['subject'] . "</td>";
    $message .= "<td>" . "New" . "</td>";
    $message .= "</tr>";

}

Open in new window

Like this
$message='';

foreach ($query as $row) {
    $message .= "<tr>"; /* <=== NOTE THE CONCATENATION (.) IN FRONT OF THE = */
    $message .= "<td>" . $row['recipientName'] . "</td>";
    $message .= "<td>" . $row['subject'] . "</td>";
    $message .= "<td>" . "New" . "</td>";
    $message .= "</tr>";

}

Open in new window

In your code, each time you loop around the $message is being overwritten by the assignment. What you want is for each iteration to add to the $message - which means every line inside the loop has to be a concatenation and not an assignment.
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
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.