How can I retrieve a single value using PDO?

This should be easy, but it's kicking me squarely in the pants and I don't know what I'm doing wrong.

Here's what I'm using:

      $sql=$conn->prepare("SELECT homepage_id from employers where id=:id");
            $sql->bindParam(':id', $id, PDO::PARAM_INT);
            $row=$sql->fetch();
            $the_name=$row->homepage_id;
            echo $the_name;

I get no errors but no value and there is a value in the database that should be coming up.

What am I doing wrong?
brucegustPHP DeveloperAsked:
Who is Participating?
 
Ryan ChongConnect With a Mentor Commented:
try:

$sql=$conn->prepare("SELECT homepage_id from employers where id=:id");
            $sql->bindParam(':id', $id, PDO::PARAM_INT);
            $sql->execute();
            $row=$sql->fetch();
            $the_name=$row['homepage_id'];
            echo $the_name;

Open in new window

0
 
Chris StanyonConnect With a Mentor Commented:
In PDO, you just need to use the fetchColumn() method after you've executed your statement:

$sql = $conn->prepare("SELECT homepage_id from employers where id = :id");
$sql->bindParam('id', $id, PDO::PARAM_INT);
$sql->execute();

$the_name = $sql->fetchColumn();

echo $the_name;

Open in new window

0
 
brucegustPHP DeveloperAuthor Commented:
Excellent!

Thank you!
0
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.

All Courses

From novice to tech pro — start learning today.