Jump to content

PDO connection with error check


LeonLatex
Go to solution Solved by kicken,

Recommended Posts

Here is my DB connection:

<?php
try {
$pdo = new PDO('mysql:host=mysqlserver.no;mydbname=mydb', 'myusername',
'mypassword');
$output = 'Database connection established.';
}
catch (PDOException $e) {
$output = 'Unable to connect to the database server: ' . $e->getMessage();
}

When it comes to error and connection check, or I start at the other end..:
I know the error check works. I print both to screen. Checked it by entering both correct and incorrect passwords. So I'm sure it works, but does it only work on the user details, or will it work on other errors as well? For example, if there is no connection with the DB server, if it is on a separate server, for example?

Link to comment
Share on other sites

<?php

$host = 'localhost'; // your database host
$dbname = 'mydatabase'; // your database name
$username = 'myusername'; // your database username
$password = 'mypassword'; // your database password

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // set additional PDO attributes if needed
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

 

  • Great Answer 1
Link to comment
Share on other sites

Strider, I am not sure what you meant with your answer. Your code does exactly the same as mine.
My code works well, that was not the problem. I was wondering something. You'll figure it out if you read my opening posting one more time.

Anyway, thanks for trying. I think you misunderstood me.

Link to comment
Share on other sites

  • Solution
2 hours ago, LeonLatex said:

but does it only work on the user details, or will it work on other errors as well

Why do you think it wouldn't work for other errors?  It says in the manual

Quote

PDO::__construct() throws a PDOException if the attempt to connect to the requested database fails

It doesn't say it throws on invalid username/password, it says it throws on failure.  Invalid username / password is only one of many reasons why the connection attempt might fail.

 

  • Great Answer 1
Link to comment
Share on other sites

40 minutes ago, kicken said:

Why do you think it wouldn't work for other errors?  It says in the manual

It doesn't say it throws on invalid username/password, it says it throws on failure.  Invalid username / password is only one of many reasons why the connection attempt might fail.

 

Yes, I see it now after you told me. Thank you for a very clear answer kicken. (another medal for you)

Link to comment
Share on other sites

3 hours ago, Strider64 said:
<?php

$host = 'localhost'; // your database host
$dbname = 'mydatabase'; // your database name
$username = 'myusername'; // your database username
$password = 'mypassword'; // your database password

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // set additional PDO attributes if needed
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

 

This is how it looks when finished.

<?php
define("HOST",'mysqlhost.no'); // Default database host.
define("USERNAME",'myusername');        // Deafault database username .
define("PASSWORD",'mypassword');            // Default database password.
define("DATABASE", 'mydatabase');    // Default database name.

function pdoConnect($dbname=DATABASE)
{
    $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    return $db;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.