LeonLatex Posted April 2, 2023 Share Posted April 2, 2023 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? Quote Link to comment https://forums.phpfreaks.com/topic/316080-pdo-connection-with-error-check/ Share on other sites More sharing options...
Strider64 Posted April 2, 2023 Share Posted April 2, 2023 <?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(); } ?> 1 Quote Link to comment https://forums.phpfreaks.com/topic/316080-pdo-connection-with-error-check/#findComment-1607015 Share on other sites More sharing options...
LeonLatex Posted April 2, 2023 Author Share Posted April 2, 2023 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. Quote Link to comment https://forums.phpfreaks.com/topic/316080-pdo-connection-with-error-check/#findComment-1607016 Share on other sites More sharing options...
Solution kicken Posted April 2, 2023 Solution Share Posted April 2, 2023 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/316080-pdo-connection-with-error-check/#findComment-1607017 Share on other sites More sharing options...
LeonLatex Posted April 2, 2023 Author Share Posted April 2, 2023 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) Quote Link to comment https://forums.phpfreaks.com/topic/316080-pdo-connection-with-error-check/#findComment-1607019 Share on other sites More sharing options...
LeonLatex Posted April 2, 2023 Author Share Posted April 2, 2023 Strider64, after kicken and I get some help from another one who came in and commented, then I saw what you tried to tell me. I have changed and using your connection now. Thank you so much, and sorry for misunderstanding you. Quote Link to comment https://forums.phpfreaks.com/topic/316080-pdo-connection-with-error-check/#findComment-1607020 Share on other sites More sharing options...
LeonLatex Posted April 2, 2023 Author Share Posted April 2, 2023 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; } Quote Link to comment https://forums.phpfreaks.com/topic/316080-pdo-connection-with-error-check/#findComment-1607023 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.