drktvjp713333
2014-10-06 13:35
浏览 75
已采纳

使用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)

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题