Matthieu McLaren
Matthieu McLaren

Reputation: 224

How to show Object Data from a Database

So I'm working on app that involves leveraging user profile data from FB. But not all users maintain the same data, so I'm using functions to determine which data is missing and then request the appropriate data from the user. These requests come from a database. A basic example of the function looks like this:

  function getEmploymentInfo () {
        if (isset($this->employer) and (!isset($this->jobtitle))) {
            $id = 1;
        } elseif (!isset($this->employer)) {
            $id = 2;
        }
        echo $this->get_profile($id);
    }

And the get profile function looks like this:

 function get_profile($id) {
$dsn = "mysql:host=localhost;dbname=software";
$username = "root"; // database username
$password = "*******"; // database password
try {
    $enter = new PDO($dsn, $username, $password);
    $sql = "SELECT response FROM getprofile WHERE response_id = ? ";
    $new_item = $enter->prepare($sql);
            $new_item->setFetchmode(PDO::FETCH_ASSOC);
    $new_item->execute(array($id));
    foreach($new_item as $nw) {
        return $nw['response'];
    }
} catch (Exception $e) {
    echo $e->getMessage();
    exit;
}
return "";
    }

And $id=1 coming from the database looks like this:

 <script type="text/javascript">

 function getJobTitle(){
document.getElementById("JobTitle").hidden = true;
document.getElementById("two").hidden = false;

}

 function getStartDate(){
document.getElementById("StartDate").hidden = true;
document.getElementById("three").hidden = false;

}

function getEndDate(){
document.getElementById("EndDate").hidden = true;
document.getElementById("four").hidden = false;

}

<?php
echo "$objUser->employer";
?>
<form action="newprofile.php" method="post">
<p><a id="JobTitle" href="#" onclick="getJobTitle()">Add Job Title</a><input       type="text" name="jobtitle" id="two" hidden="true" value="Add Job Title"></input></p>
<p><a id="StartDate" href="#" onclick="getStartDate()">Add Start Date</a><input    type="text" name="startdate" id="three" hidden="true" value="Add Start Date"></input></p>
<p><a id="EndDate" href="#" onclick="getEndDate()">Add End Date</a><input type="text"   name="enddate" id="four" hidden="true" value="Add End Date"></input></p>
<input type="submit" value="submit"></form>

But when this code returns from the database to the page echo "$objUser->employer"; doesn't populate. Meanwhile if I write that code directly on the page it works. What gives?

Upvotes: 0

Views: 147

Answers (1)

Rylab
Rylab

Reputation: 1295

Databases just store text, not actual instantiated objects. The returned value has no way of knowing what $objUser was when you stored all that text.

There's a lot of things wrong with the way you're trying to go about doing this, you shouldn't be storing all that code in the database for every row. But the most simple way to answer this and point you in the right direction, is that you need to serialize objects in order to store them in the database, and unserialize them after pulling the record out of the database, in order to use them again.

Upvotes: 1

Related Questions