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 trialkrishna sapkal
Courses Plus Student 62 Pointswhy database is not connecting?
hers my code
<?php
try{
$db = new PDO("mysql:host=localhost;dbname=feedback;port=8889","root","krishna");
}
catch(Exception $e){
echo 'could not connect to the database';
exit();
}
echo'wooo hoo';
?>
4 Answers
krishna sapkal
Courses Plus Student 62 PointsGot it i entered the wrong port no thank you all
Codin - Codesmite
8,600 PointsTry adding a confirmation that the connection was successful, there does not seem to be anything wrong with the syntax of your connection. You may also need to increase the error level to see a more detailed reason why it is not connecting as I believe by default PDO does not fire exceptions on error.
<?php
try{
$db = new PDO("mysql:host=localhost;dbname=feedback;port=8889","root","krishna");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); /* Add this line */
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); /* Add this line */
echo 'connected to the database'; /* Add this line */
}
catch(Exception $e){
echo 'could not connect to the database';
exit();
}
echo'wooo hoo';
?>
What errors are you getting and or what is not working in your code?
missgeekbunny
37,033 PointsWhy do you have quotes around mysql to the port?
krishna sapkal
Courses Plus Student 62 Pointsbeacuse php sends a string to the database for connection
KnowledgeWoods Consulting
5,607 PointsRemove the port