普通网友 2013-09-23 14:05
浏览 128

php连接到mysqlworkbench localhost

I have a mac with OSX 10.8.4. I have installed my localhost and it works just fine. I have made a php script, from where I would like to connect MySQL workbench database to. My apache tomcat server runs, and also mysql on the computer, and I use XAMPP. This is my code:

    <?php 

 // Establish connection to DB using PDO
 try {
  $pdo = new PDO('127.0.0.1:3306', 'root', '');
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $pdo->exec('SET NAMES "utf8"');
  echo "Connected!";
 } catch (PDOException $e) {  
  $error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
  echo "Connection failed";

  exit();
 }

I have tried this script to connect to a remote mysql server, where it works fine, but I cannot use it for my localhost. I also tried just to put in localhost in new PDO, but still the same. Does anybody have a clue to what is wrong?

Best Regards Mads

  • 写回答

1条回答

  • dpmwy80068 2013-09-23 14:10
    关注

    You'll have an easier time knowing what's not working if you echo the exception being thrown.

    Your code

    } catch (PDOException $e) {  
      $error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
      echo "Connection failed";
    }
    

    doesn't actually print the exception! Try this instead:

    } catch (PDOException $e) {
        $error = 'ERROR - Connection to DB failed: ' . $e->getMessage();
        echo $error;
    }
    

    That will at least give you some helpful debugging info.

    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试