【发布时间】:2014-10-06 22:33:39
【问题描述】:
我已成功连接到我的 MySQL 数据库并使用此代码获取数据:
try {
$results = $db->prepare("SELECT * FROM persoonsinfo WHERE id = ?");
$results->bindParam(1,$_SESSION["user_id"]);
$results->execute();
} catch (Exception $e) {
echo "data could not be retrieved";
exit;
}
使用var_dump($results->fetch(PDO::FETCH_ASSOC)); 转储正确的行!例如:
array(15) {
["id"]=>
string(1) "1"
["bedrijf"]=>
string(0) ""
["titel"]=>
string(0) ""
["voorletters"]=>
string(4) "test"
["tussenvoegsel"]=>
string(0) ""
["achtervoegsel"]=>
string(0) ""
["meisjesnaam"]=>
string(0) ""
["thuis_adres"]=>
string(22) "test"
["thuis_postcode"]=>
string(7) "test"
["thuis_plaats"]=>
string(9) "test"
["thuis_land"]=>
string(0) ""
["mobiele_telefoon"]=>
string(11) "test"
["email_thuis"]=>
string(20) "test"
["geboortedatum"]=>
string(0) ""
["bsn_nummer"]=>
string(0) ""
}
那就太完美了,但现在我正在为如何将数据回显到屏幕上而头疼。
我创建了这个:$user_info = $results->fetch(PDO::FETCH_ASSOC);,并期待这个 <?php echo $user_info['voorletters']; ?> 能够工作,但它没有任何回应,这让我发疯了。
我正在尝试在我的“page-mijnawf.php”文件中执行echo,并且在该文件中我执行了:
<?php include (TEMPLATEPATH . '/includes/database.php'); ?>
包括上面显示的代码。
我在这里缺少什么? 谢谢!
更新 01
这里的要求是我更新的代码。我将发布三个块,我的database.php(数据库连接部分)user-session-info.php(获取用户ID 的会话)和page-mijnawf.php(用户可见的前端页面):
这是database.php:
try {
$db = new PDO("mysql:host=localhost;dbname=contacten_db;port=8889","root","root");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to the Database Contacten_DB";
exit;
}
try {
$results = $db->prepare("SELECT * FROM persoonsinfo WHERE id = ?");
$results->bindParam(1,$_SESSION["user_id"]);
$results->execute();
} catch (Exception $e) {
echo "data could not be retrieved";
exit;
}
echo "<pre>"; //For Testing only
$user_info = $results->fetch(PDO::FETCH_ASSOC);
var_dump($user_info);
?>
还有user-session-info.php:
<?php
// Start the session
session_start();
// Set session variables
$_SESSION["user_id"] = intval(get_current_user_id());
//echo "User ID =" . " " . $_SESSION["user_id"]
?>
还有page-mijnawf.php,因为 99% 的内容与本主题无关,所以我删除了它:
<div>
<?php include (TEMPLATEPATH . '/includes/database.php'); ?>
<?php include (TEMPLATEPATH . '/includes/user-info-session.php'); ?>
<p class="no-bottom-margin">Welkom <?php echo $user_info['voorletters']; ?> Uw Fonds: </p>
</div>
请注意上面代码块中关闭 div 之前的行中的 <?php echo $user_info['voorletters']; ?>。那条线没有回应
感谢您的帮助!
【问题讨论】:
-
(顺便说一句,如果你在输出一个div之后包含了user-info-session.php文件(它调用了session_start()),你会得到一个错误)