Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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