0

Hello.
Please look at this:

<?php
    $servername = "localhost";
    $dbname = "mydbname";
    $dbusername = "mydbusername";
    $dbpassword = "mydbpassword";

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

        $id=$_GET['id'];

        $sql = "DELETE FROM Posts WHERE ID = :id";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $id, PDO::PARAM_STR);
        $stmt->execute();
        header("location: admin.php");
    } catch(PDOException $e) {
        echo $sql . "<br>" . $e->getMessage();
    }            
    $conn = null;
?>

I see some tutorial examples put this part in a for example "connection.php" file:

        <?php
        $servername = "localhost";
        $dbname = "mydbname";
        $dbusername = "mydbusername";
        $dbpassword = "mydbpassword";

        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        ?>

and then using include(connection.php); in their php files. Now if you look at my code above, you see there is this line in it:

        try {

So, what should i do? Can i forget to use try-catch in my scripts? If not so how can i put that part of code in the connection.php file to use with include()?

Can i put this:

    <?php
        $servername = "localhost";
        $dbname = "mydbname";
        $dbusername = "mydbusername";
        $dbpassword = "mydbpassword";

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

in the connection.php file while the try block left open?! and be closed in the other php files?

Edited by Niloofar24

3
Contributors
6
Replies
26
Views
2 Years
Discussion Span
Last Post by Niloofar24
0

The whole point (IMO) of try is to catch an exception when things go wrong. If you're not using a catch then why do you need a try?

Think carefully about howw you use Exceptions in production code. Exceptions are different to errors, but there are methods of combining them to give a consistent output, if you need to. Production uses could include writing to logs etc.

Edited by diafol

3

Wonder if I get the question correctly, you wish to seperate your full code into conn.php but wonder on how to made the try catch block to seperate itself? If so, try to understand what you wish to accomplish and try to understand the codes you written. If I am in the case, I will put into conn.php

<?php
    $servername = "localhost";
    $dbname = "mydbname";
    $dbusername = "mydbusername";
    $dbpassword = "mydbpassword";
    try {
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $dbusername, $dbpassword);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    } catch(PDOException $e) {
        //here will only display the connection error
        echo "Database connection error:" . $sql . "<br>" . $e->getMessage();
    }
?>

Then my code in my main script will be

<?php
    include('conn.php');
    try {
        $id=$_GET['id'];
        $sql = "DELETE FROM Posts WHERE ID = :id";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':id', $id, PDO::PARAM_STR);
        $stmt->execute();
        header("location: admin.php");
    } catch(PDOException $e) {
        echo "Query error:".$sql . "<br>" . $e->getMessage();
    }            
    $conn = null;
?>
Votes + Comments
Ah. Now I get the question
0

Can you goes detail on what error you get? As far as I know, the echo "Database connection error:" . $sql . "<br>" . $e->getMessage(); and echo "Query error:".$sql . "<br>" . $e->getMessage(); most probably not work as we wish, as $sql is not defined but those messages I simply shows as example. Please do understand and implement your own. Or there are other errors occurs, can you elaborate more on them?

Edited by lps

0

Oh! i'm sorry i found the problem. I forgot to change this part:

$servername = "localhost";
$dbname = "mydbname";
$dbusername = "mydbusername";
$dbpassword = "mydbpassword";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.