Php 如何显示数据库中的对象数据
所以我正在开发一款应用程序,它涉及利用FB的用户档案数据。但并非所有用户都维护相同的数据,因此我使用函数来确定缺少哪些数据,然后向用户请求适当的数据。这些请求来自数据库。函数的基本示例如下所示:Php 如何显示数据库中的对象数据,php,oop,Php,Oop,所以我正在开发一款应用程序,它涉及利用FB的用户档案数据。但并非所有用户都维护相同的数据,因此我使用函数来确定缺少哪些数据,然后向用户请求适当的数据。这些请求来自数据库。函数的基本示例如下所示: function getEmploymentInfo () { if (isset($this->employer) and (!isset($this->jobtitle))) { $id = 1; } elseif (!isse
function getEmploymentInfo () {
if (isset($this->employer) and (!isset($this->jobtitle))) {
$id = 1;
} elseif (!isset($this->employer)) {
$id = 2;
}
echo $this->get_profile($id);
}
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 "";
}
<script type="text/javascript">
function getJobTitle(){
document.getElementById("JobTitle").hidden = true;
document.getElementById("two").hidden = false;
获取配置文件函数如下所示:
function getEmploymentInfo () {
if (isset($this->employer) and (!isset($this->jobtitle))) {
$id = 1;
} elseif (!isset($this->employer)) {
$id = 2;
}
echo $this->get_profile($id);
}
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 "";
}
<script type="text/javascript">
function getJobTitle(){
document.getElementById("JobTitle").hidden = true;
document.getElementById("two").hidden = false;
来自数据库的$id=1如下所示:
function getEmploymentInfo () {
if (isset($this->employer) and (!isset($this->jobtitle))) {
$id = 1;
} elseif (!isset($this->employer)) {
$id = 2;
}
echo $this->get_profile($id);
}
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 "";
}
<script type="text/javascript">
function getJobTitle(){
document.getElementById("JobTitle").hidden = true;
document.getElementById("two").hidden = false;
}
}
但是当该代码从数据库返回到echo页面“$objUser->employer”时;没有填充。同时,如果我直接在页面上编写代码,它就会工作。数据库只存储文本,而不是实际的实例化对象。当您存储所有文本时,返回的值无法知道$objUser是什么
您尝试这样做的方式有很多问题,您不应该在数据库中存储每一行的所有代码。但回答这个问题并为您指出正确方向的最简单方法是,您需要序列化对象以将其存储在数据库中,并在将记录从数据库中取出后取消序列化,以便再次使用它们。这些对象在其他地方实例化。我只是从数据库里给他们打电话。要做到这一点,有什么更简单的方法呢?