dst8922 2013-11-16 07:32
浏览 135
已采纳

如何从我的localhost连接到我的数据库sql文件?

I am trying to connect to my database through php. I exported my php database as an sql file as told. I tried to go to connect to it and show a message that i am connect. However I'm not.

Im running my php in netbeans and the file is located in wamp > www

it says ERROR: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES)

<?php
try {
$conn = new 
PDO('mysql:host=localhost;dbname=isad235', "root", 
"root");
echo "CONNECTED";
$sql = "SELECT * FROM members";
foreach($conn->query($sql) as $row)
{
  echo $row;
}
}catch(PDOException $e)
{
 echo 'ERROR: '.$e->getMessage();
}
?>
  • 写回答

3条回答 默认 最新

  • duanguane1670 2013-11-16 07:36
    关注

    this is working for me. It it doesnt work you need to check your credentials.

    class db {
        public static function dbFactory($host, $dbase, $user, $pass) {
            $pdo = new PDO("mysql:host=$host;dbname=$dbase", $user, $pass);
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);        
            return $pdo;
        }
    }
    $db = db::dbFactory('localhost','mydbname','myusername','mypassword');
    

    by the way, you cannot connect to an sql file. You need to import it into your dbms. After that you should be able to connect.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 vue请求不到数据,返回状态200,数据为html
  • ¥15 访问url时不会自动调用其 Servlet的doGet()
  • ¥15 用白鹭引擎开发棋牌游戏的前端为什么这么难找
  • ¥15 MATLAB解决问题
  • ¥35 哪位专业人士知道这是什么原件吗?哪里可以买到?
  • ¥15 关于#c##的问题:treenode反序列化后获取不到上一节点和下一节点,Fullpath和Handle报错
  • ¥15 一部手机能否同时用不同的app进入不同的直播间?
  • ¥20 输入import torch显示Intel MKL FATAL ERROR,系统驱动1%,: Cannot load mkl_intel_thread.dll.
  • ¥15 点云密度大则包围盒小
  • ¥15 nginx使用nfs进行服务器的数据共享
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部