drktvjp713333 2014-10-06 13:35 采纳率: 0%
浏览 93
已采纳

使用fetch(PDO :: FETCH_ASSOC)回显来自MySQL db的数据;

I've successfully connected to my MySQL db and fetched data using this code:

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;
}

using the var_dump($results->fetch(PDO::FETCH_ASSOC)); dumps the correct row! for example:

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) ""
}

so thats perfect, but now I'm hurting my head over how to echo data to the screen.

I created this: $user_info = $results->fetch(PDO::FETCH_ASSOC); and was expecting this <?php echo $user_info['voorletters']; ?> to work, but it doesn't echo anything and it's driving me crazy.

Im trying to do the echo in my `page-mijnawf.php' file and in that file I did:

<?php include (TEMPLATEPATH . '/includes/database.php'); ?>

to include the code shown above.

What am I missing here? Thanks!

UPDATE 01

As requested here is my updated code. Ill post three blocks, my database.php (database connection part) user-session-info.php (a session to get the userID) and page-mijnawf.php (the front-end page that is visible to the user): here is 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);

?>

And 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"]
?>

and page-mijnawf.php which I stripped because 99% of it's content is irrelevant to this topic:

<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>

Notice the <?php echo $user_info['voorletters']; ?> in the line before the closing div in the code block right above here. That line echos nothing

Thanks for helping!

  • 写回答

1条回答 默认 最新

  • doujie3888 2014-10-06 13:38
    关注

    The "fetch" method returns the contents from the database and then skips to the next row. If you have just one row, then calling it once for the "var_dump" will already reach the end of the result set.

    Your solution: do not call var_dump($results->fetch(PDO::FETCH_ASSOC));, in order to execute $results->fetch just once!

    If you need to print the record on screen for debug, then after calling $user_info = $results->fetch(PDO::FETCH_ASSOC); you can do var_dump($user_info)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料