2

Im trying to convert all my mysql code into PDO. First thing Im trying to convert to PDO is my database connection. Can somebody help me on the right way?....

Here comes my database connection in mysql:

  $host = "localhost"; 
$user = "root"; 
$password = "root";
$db = "blog";

$bd = mysql_connect($host, $user, $password) or die("Opps something wrong...");
mysql_select_db($db, $bd) or die("Opps something wrong...");
3
  • Read a nice tutorial on PDO. Commented Mar 26, 2013 at 8:26
  • You cannot short-cut on this, you really do have to read the manual and cut aside some time to test out all the options, especially the parts about prepared statements. You have to bang this stuff into your head. Commented Mar 26, 2013 at 8:35
  • @Mihai lorga Thanks, I will take a look on that tutorial Commented Mar 26, 2013 at 9:11

5 Answers 5

2

There is a PDO tag wiki where you can learn the right way (as well as many other useful things) from someone who had a real experience with PDO.

$dsn = "mysql:host=$host;dbname=$db;charset=utf8";
$opt = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);
$pdo = new PDO($dsn,$username,$password, $opt);

Unlike all other codes which are useless for handling connection errors (as they set exception mode after the actual connect) it will

  • make PHP throw an exception on connection error
  • will not reveal sensitive information to a potential attacker on a live server by echoing an error out.
  • provide you with indispensable stack trace.
  • set the connection encoding in the right place

Unlike your old code it will not die with useless message but it will either

  • die with useful error message on-screen
  • die with useful error message logged, leaving screen blank
  • die with useful error message logged, with conventional error page shown
  • not die at all but gracefully handled with designated handler function

... depends on the chosen settings.

Sign up to request clarification or add additional context in comments.

2 Comments

Why don't you add this as a note to the php.net manual page? ;)
@Your Common Sense Many thanks, your code is working. Now I have to convert all my other code using PDO...
2

Have a look at the PHP Manual's PDO page, especially the part on 'Connections and Connection Management': http://www.php.net/manual/en/pdo.connections.php

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);

or... use Captain Common Sense's approach :)

Comments

1

Connection with error handling..

try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

4 Comments

Thanks, nice with a error handling to... Im gonna try your code.
It has nothing to do with error handling. And it will never raise an error on connect.
May i know how come it would never possible raise an error on connection?
@Kaii sure. Just try it and see.
0
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);

OR

try {
    $conn = new PDO('mysql:host=localhost;dbname=blog', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}

More details Click here

Comments

0

Have you looked into the manual?

$dbh = new PDO('mysql:host=localhost;dbname=yourdbname', 'youruser', 'yourpassword');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.