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
Anders Thomsen
1,995 PointsPHP SQL empty array, but no error
Hi there
Im trying to follow along the PHP SQL database course, incorporating thing in my own site as I go a long..
I have uploaded the database and seem to be able to connect, but when I run the "var_dumb" all I get is an empty array:
array(0) { }
My code:
try { $db = new PDO("mysql:host="my info""); $db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $db->exec("SET NAMES 'utf8'"); } catch (Exception $e) { echo "Could not connect to the database."; exit; }
try { $dest = $db->query("SELECT var1, var2 FROM table"); } catch (Exception $e) { echo "Data could not be loaded from database"; exit; }
$result = $dest->fetchAll(); echo "<pre>"; var_dump($result); echo "</pre>"; ?>
I have checked the names and also tried running a "SELECT * from table" as an alternative.
Would love your help with this.
br Anders
EDIT: Turns out there was a an error in the table why nothing was reported - now it works :)
2 Answers
Andrew Shook
31,709 PointsAnders, without seeing all of you code or how your database is structured my guess is that there is a problem with your query statement. In your select statement you have var1 and var2 but I don't see where you bound those parameters to the query. Right now it looks like the select statement is looking for something called var1 and var2.
Anders Thomsen
1,995 PointsHI Andrew, thanks for getting back to me.
Since the current project is somewhat secret I cant share to many details. There is really not that much more code beside the what is shown here.
The "var1" and "var2" are variables in the table within the database that both exists in the given table. Do I need to any further to make this be included in the $result array?