Report this

What is the reason for this report?

PDO connect DB Failed (Ubuntu 12.0.4 LAMP)

Posted on April 18, 2014

Hi. I am trying to connect to a database (i have setup using phpmyadmin) thru a script (i.e.config script) i have placed in the “home/user" directory whereas the PHP GET script is placed in the “var/www" directory. When i run the GET script that attempt to connect to the database via config script, connection did not work and no error being echo. I know my GET script has successfully link to config script because i received the echo “config". My scripts have been tried out successfully in other web servers.

Do i need to install PDO or ? Badly need advices on this.

–config script— <?php

$db_host = “localhost”; $db_name =“xxxxxxxxx"; $username = “xxxxxxxxx"; $password = “xxxxxxxxxxxxx";

$dsn = “mysql:host=$db_host;dbname=$db_name;charset=utf8”; $opt = array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ); echo "config”; //i can see this echo from the browser try { $dbh = new PDO($dsn,$username,$password, $opt); echo $dbh; //cannot see this echo } catch (PDOException $e) { echo 'Connection failed: ’ . $e->getMessage(); }

// create LM object, pass in PDO connection $lm = new lazy_mofo($dbh); echo $lm; ?>



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Try installing the PDO extension and the MySQL PDO driver: <br><pre>sudo apt-get install php5-pdo php5-mysql</pre>

it works! Thanks!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.