2017-06-24 31 views
0

我为我的MySQL数据库使用免费托管,但是当我尝试通过我的PHP代码连接到它时,它会引发未捕获的PDOException错误,并且我的访问被拒绝。我似乎无法找到为什么连接被拒绝,因为我的凭证是正确的。如何解决未捕获的PDOException?

我正在使用Ionic 2框架,如果有帮助。

错误:

<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连接代码:

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

孔捕捉它亲爱的丽莎 – RiggsFolly

+0

你是什么意思? –

+0

猜你会和我一样知道同一首童谣 – RiggsFolly

回答

1

捕获异常,最好的办法是把你的代码在try catch块。但在你的情况下,我认为你的密码不正确。导致错误(例外)的代码应该放在try块中,而不是像这样捕获它。

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

你可以阅读更多关于异常处理here

相关问题