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

PHP

why 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

Got it i entered the wrong port no thank you all

Codin - Codesmite
Codin - Codesmite
8,600 Points

Try 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
missgeekbunny
37,033 Points

Why do you have quotes around mysql to the port?

beacuse php sends a string to the database for connection

Remove the port