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!