Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kane Stoboi
21,919 PointsPage not loading once function is called.
I'm calling a function to delete all data within a MySQL table using the code below.
<?php
require_once("../includes/config.php");
require_once(ROOT_PATH . "includes/functions.php");
delete_reference_list();
$referenceList = get_reference_list_harvard();
include(ROOT_PATH . "includes/header.php");
?>
<div id="wrapper">
<h2>Reference List</h2>
<?php
foreach ($referenceList as $reference) {
echo "<div class='reference_list'>" . $reference . "</div>";
}
?>
</div>
</body>
</html>
Here's the function being called from functions.php
<?php
function delete_reference_list() {
require_once(ROOT_PATH . "includes/database.php");
try {
$db->query("DELETE FROM reference_list");
echo "success";
} catch (Exception $e) {
echo $e;
}
return null;
}
?>
It seems that if I remove the $referenceList variable the page works but if I have both $referenceList and the delete_reference_list() function there is some sort of error somewhere.
Kane Stoboi
21,919 PointsThe page just loads "success" and does not load any further
Kane Stoboi
21,919 PointsI'm getting this php error:
Notice: Undefined variable: db in /Applications/MAMP/htdocs/reference/includes/functions.php on line 854
Fatal error: Call to a member function prepare() on a non-object in /Applications/MAMP/htdocs/reference/includes/functions.php on line 854
Here's the code between 849 and 860
<?php
function get_reference_list_harvard() {
require_once("database.php");
try {
$query = $db->prepare("SELECT * FROM reference_list"); //(line 854)
$query->execute();
$results = $query->fetchall(PDO::FETCH_ASSOC);
} catch (Exception $e) {
echo "Data could not be retreived from the database: " . $e;
exit;
}
and the database file
<?php
try {
$db = new PDO("mysql:host=localhost;dbname=reference_list;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";
exit;
}
Christian Andersson
8,712 Pointsupdated my answer.
1 Answer
Christian Andersson
8,712 Pointsif it doesn't recognize $db as an object, then there might be something wrong with the constructor in your database file.
Also, is your table-name same as the database-name (reference_list)? If not an error, that's an unusual practice.
Kane Stoboi
21,919 PointsThanks for you help Christian, I've been calling require_once to include the database file instead of require which is why the $db object was not being constructed correctly. Thanks for raising my database name issue, I built it a few days ago and must have overlooked it.
Christian Andersson
8,712 PointsChristian Andersson
8,712 Pointswhat error are you getting?