Hi😊 I am new here on Devsense and thank you for the membership here on Devsense.

I got a problem with my new PDO connection. I wanted to change it because I want it to print to screen the error code and an error message if there is no contact with MySQL DB, and give a message if it's connected. But what has happened is like nothing. Or, the connection script won't connect. It writes out the message on the screen.

I have used some hours trying to fix the problem but without luck. So, I ask you "gurus" to be so kind and look over it for me and fix the problem for me? I can't manage to fix it myself. Please😊

<?php
define("host",'myhost'); // Default database host.
define("dbname", 'mydbname');   // Default database name.
define("username",'myusername');	// Deafault database username .
define("password",'mypassword');		    // Default database password.

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;
}
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();
}
?>

Leon I am new here on Devsense

FYI, you are at phpbuilder, not Devsense here. πŸ˜‰

Anyway, do you actually output $output anywhere?

    Hi, thank you for the answer. Yes I am <?=$output?> this in index.php.

    They didn't answer me on Devsense, so i copied my text from there and paste it here on phpBuilder. Or, Devsense was for dev-tools, so i didn't post anything there.
    Sorry for confusing you.

      Hard to say looking at that what is "wrong", but maybe some other exception is happening. Perhaps you should catch that, too?

      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();
      } catch (Exception $e) {
        $output = 'Uh-oh, something else happened: ' . $e->getMessage();
      }
      

        Thanks for the tip NogDog. That's a very good tip. I will try that now, and soon come back.

          I tried and it failed.... again. 2Xtimes the same error message:

          Unable to connect to the database server: SQLSTATE[HY000] [2002] Connection refused

          I have checked all my user data like password, host address, DB name and username. There is something in the script i dont figure out, but what?

            Okay...I'm confused. I thought you were originally saying that the problem was that nothing was being output; but now you are saying you do, in fact, get an error. πŸ˜•

            If you google the SQLSTATE[HY000] [2002] Connection refused message, it appears to indicate that either you cannot connect to the specified database server for some reason, or it is not listening for requests on the default port number that PHP/PDO is assuming. If the latter, then you need to specify the desired port number as part of the PDO dsn string, e.g. something like:

            'mysql:host=mysqlserver.no;port=6789;mydbname=mydb' (changing the 6789 to whatever the correct port number should be)

            Otherwise, verify that the host name is correct and that it is accessible from wherever you are testing this?

            No, the script is working that way it is sending an error message since it can't connect, and there it stops. I am going thru the script to find the error, but I can't find it. I can't find out why it won't connect. The password, host, database name and user name is correct, So, there must be something with the script it self. But where?

              Is the pdoConnect function being used for anything? Or is that left over from some other attempt?

              Have you confirmed that the db server is running and running where you think it is? The 2002 error is a client-side code and comes from the connection library; getting an error message from the client doesn't mean the server is running.

              Can you log in manually using exactly the same credentials (using whatever values for mysqlserver.no, mydb, myusername and mypassword the script is supposed to be using)? If you can then that would go further to narrowing the problem down to your script.

              • Leon replied to this.

                Weedpacket All user data is checked and correct. The server is not locally or a virtual webserver/PHP server/DB server. This is a server in a data center here in Norway. I will start over again making a new PDO connection. Anyway, the same user data is working in MySQL Workbench and PHP MyAdmin. So, it's something wrong with the PDO connection script.

                Leon You should consider using SSH to log into the server where you are running this PHP code and attempt a connection to the mysql server from the command line.

                mysql -h mysqlserver.no -u myusername -p

                enter your password when prompted. if you are able to connect to the server, then select the database at the SQL prompt

                use mydb;

                If you can perform all these steps, then you can probably be at least make sure it is possible to connect to the mysql server from that machine. if you cannot connect, you should get some better idea of where the failure lies.

                • Leon replied to this.
                  Write a Reply...