0

I'm using a free hosting solution for my MySQL database. However, when I try to connect to it through my PHP code it throws me a uncaught PDOException error and my access is denied. I can't seem to find why the connection is denied as my credentials are correct.

I'm using Ionic 2 framework, if that helps.

Error:

<b>Fatal error</b>:  Uncaught PDOException: SQLSTATE[HY000] [1045] Access denied for user 'sql10181809'@'153.92.0.10' (using password: YES) in /storage/ssd5/403/2053403/public_html/escanerproducto.php:33
Stack trace:
#0 /storage/ssd5/403/2053403/public_html/escanerproducto.php(33): PDO-&gt;__construct('mysql:host=sql1...', 'sql10181809', '\xE2\x81\xA0\xE2\x81\xA0\xE2\x81\xA0dfFf6l...')
#1 {main}
  thrown in <b>/storage/ssd5/403/2053403/public_html/escanerproducto.php</b> on line <b>33</b><br />

PHP connection code:

<?php
    @$db = new PDO("mysql:host=sql10.freesqldatabase.com;dbname=sql10181809", "sql10181809", "PASS");

    if($db){
        $sql = "select * from producto WHERE
                    codigo_barra='" . $codigodebarra . "'";

        $query = $db->prepare($sql);
        $query->execute();
        $query->setFetchMode(PDO::FETCH_NUM);
        if($fila = $query->fetch()){
            $nombre = $fila[2];
            $resultados_finales = array("nombre"=>$nombre);
            echo json_encode($resultados_finales);
        }
        else{
            $resultados_finales = array("mensage"=>"error");
            echo json_encode($resultados_finales);
        }
    }
    else {
        $resultados_finales = array('mensage' => "ERROR.");
        echo json_encode($resultados_finales);
    };
?>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Midori_hige
  • 311
  • 6
  • 21
  • 1
    Maybe even follow the basic stuff you find in the manual about TESTING for errors or CATCHING Exceptions **[The manual.it should be your first port of call not you last](http://php.net/manual/en/pdo.construct.php)** – RiggsFolly Jun 24 '17 at 23:03
  • That error message usually means you have the password wrong. – RiggsFolly Jun 24 '17 at 23:06
  • 1
    The @ in `@$db = new PDO...` means "silence errors". It may not make any difference in this case, but when trying to understand an error, silence is the last thing you want. Your sample code is also a long way from being a [mcve]; simplify it as much as possible, and you'll be focusing on the part that's actually causing the problem. – IMSoP Jun 24 '17 at 23:08
  • When using a localhost it connects just fine. – Midori_hige Jun 24 '17 at 23:33
  • The problem is with your password – Saqlain Jun 25 '17 at 02:07
  • Whoa! [PDO exposes the first 16 characters of the password](https://stackoverflow.com/questions/6455018) (all of it in this case as there are only 11 characters in the password (way too short for a brute-force attack)). That is one more reason to [have a long password over one with high entropy](https://explainxkcd.com/wiki/index.php/936:_Password_Strength) (as PHP makes you lose the first 16 characters, something with 40 or more would be a good idea). – Peter Mortensen Apr 21 '20 at 02:50

1 Answers1

2

The best way to catch the exception is to put your code in a try-catch block. But in your case I think you are putting the password in incorrect. The code which causes error (exception) should be in try block and then catch it like so:

try{
   $db = new PDO("mysql:host=sql10.freesqldatabase.com;dbname=sql10181809",
                 "sql10181809", "PASS");
   $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
    echo $e->getMessage() . '  ' . '   ' . getCode() . '  ' . getLine() . '
      ' . getFile();
}

You can read more about exception handling here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Saqlain
  • 465
  • 3
  • 11
  • Why is there a newline in the middle of one of the literal strings in the line with "getMessage" (the last literal string)? Should the line break be there? – Peter Mortensen Apr 21 '20 at 02:44