普通网友 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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀