1

If I want to put the connection in an external file, what part of this code should be in that external file?

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "podcast";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $sql = "UPDATE bookmarks
            SET podcast=122, text='some text'
            WHERE id = 152";

    $stmt = $conn->prepare($sql);
    $stmt->execute();
    echo $stmt->rowCount() . " records UPDATED successfully";
}

catch(PDOException $e){
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
Nrc
  • 9,577
  • 17
  • 67
  • 114

2 Answers2

2

This part will go in external file e.g connection.php

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "podcast";
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>

and then your code will look like

require("connection.php");

try {

    $sql = "UPDATE bookmarks
            SET podcast=122, text='some text'
            WHERE id = 152";

    $stmt = $conn->prepare($sql);
    $stmt->execute();
    echo $stmt->rowCount() . " records UPDATED successfully";
}

catch(PDOException $e){
    echo $sql . "<br>" . $e->getMessage();
}

$conn = null;
Shehary
  • 9,926
  • 10
  • 42
  • 71
  • Whouldn't be correct to put try and catch in the external file? why? – Nrc Jul 18 '15 at 14:33
  • this will answer your question, i not gona type the whole story http://stackoverflow.com/questions/14973642/how-using-try-catch-for-exception-handling-is-best-practice – Shehary Jul 18 '15 at 14:36
  • I am very new. I am just learning from an online tutorial. I have been testing and it seems that to detect possible errors code must be inside try. Do I understand well? – Nrc Jul 19 '15 at 08:38
  • Yes you understand well but the best way to detect errors follow AK-Sonu Note in his answer, its the best practice. In addition of AK-Sonu Note, i like to add that either you can add the error code into the file you are currently working on to check error or you can add it in file which is common and `include` or `require` in all of your PHP files, e.g `connection.php` obviously you need to include this file in other PHP files for code to process. – Shehary Jul 19 '15 at 08:59
  • In addition to above comment, `ini_set('display_errors',1);` means It will display error, if you change it to `ini_set('display_errors',0);` It will not display the error but will log the error into `errorlog` file, so my experience, when done with code, set it to `0` but never removed it and always keep an eye on `errorlog` file for errors, so if anything goes south you will know. :) – Shehary Jul 19 '15 at 09:09
  • This seems great!. I can put that error in connection.php and then remove try, catch and remove that line: $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); is that correct? – Nrc Jul 19 '15 at 09:59
  • No, `error_reporting(E_ALL); ini_set('display_errors',1); ` is additional check to check the code, may b you have error somewhere else in your code, lets say you made `typo mistake` somewhere, try / catch won't tell you that you made a typo mistake but this `error_reporting(E_ALL); ini_set('display_errors',1); ` will tell you, additionally about try/catch please read here, it explains it briefly http://code.tutsplus.com/tutorials/php-database-access-are-you-doing-it-correctly--net-25338 – Shehary Jul 19 '15 at 10:08
0
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "podcast"; 
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}

Till this part can go to external file, and can be used to open a connection where ever required.

  • Why to put the try in the external file? why other people don't? I do not understand – Nrc Jul 18 '15 at 15:03