douzhu7507 2013-11-03 15:06
浏览 53

在PHP中,需要花费大量时间来捕获异常

I give you something interesting here since I researched all stackoverflow but nobody had ever asked for a similar issue! Let's go with the description:

I'm trying to connect a simple mysql database to my server using php. I can say from now that the issue is present with both the PDO and the mysql_connect methods. Now I'll explain to You:

In the typical "TRY-CATCH" block I want to test the Error Message for some exceptions. So I did something like the following:

try {
$db = new PDO("mysql:host=localhost;dbname=shirts4mike;","root","ffredda");
$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db -> exec("SET NAMES 'utf8'");
} catch (Exception $e) {
echo "Could not connect to the database.";
exit;
}

Now, to test the error message, I tried to change the host to:

host=Wronghost;

and refreshed the page running on a local server (apache2).

And here we go with the problem! It takes something like two minutes before my browser catches the exception and loads the error message.

I'm becoming crazy with this! I don't know on what it depend! I think it's the server trying multiple times to connect to the database before giving up and throw the Exception! But how to limit this number of attempts if this is the problem? And if this isn't the problem someone has an idea about where to look to find and solve it? Who is the responsible? Mysql? Apache? The PHP code (which I doubt)?

I would really appreciate any help. Really. Thank You anwyway and hope to hear from someone soon!

--Fabrizio

  • 写回答

1条回答 默认 最新

  • douao8204 2013-11-03 15:17
    关注

    You have to use proper credentials when connecting to database.

    With setting proper hostname connect will be blazingly fast and problem solved.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)