无法使用PDO连接到MySQL服务器 [英] Not able to connect to MySQL server using PDO

查看:154
本文介绍了无法使用PDO连接到MySQL服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP脚本,我用来连接到MySQL数据库。通过mysql_connect连接工作完美,但是当使用PDO时,我得到以下错误:

  SQLSTATE [HY000]服务器主机'hostname'(3)

我使用的代码如下:

 <?php 
ini_set('display_errors',1);
error_reporting(E_ALL);

$ hostname_localhost =hostname;
$ database_localhost =dbname;
$ username_localhost =user;
$ password_localhost =pass;
$ user = $ _GET ['user'];
$ pass = $ _GET ['pass'];

try {
$ dbh = new PDO(mysql:host = $ hostname_localhost; dbname = $ database_localhost,$ username_localhost,$ password_localhost);
echo'Connected to DB';
$ dbh-> setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);
$ stmt = $ dbh-> prepare(SELECT check_user_company(:user,:pass));
$ stmt = $ dbh-> bindParam(':user',$ user,PDO :: PARAM_STR,16);
$ stmt = $ dbh-> bindParam(':pass',$ pass,PDO :: PARAM_STR,32);

$ stmt-> execute();

$ result = $ stmt-> fetchAll();

foreach($ result as $ row)
{
echo $ row ['company_id']。'< br />';
}

$ dbh = null;
}
catch(PDOException $ e)
{
echo $ e-> getMessage();
}
?>

提前感谢

解决方案

遇到同样的问题。 Mine解决方案是另一个数据库端口。我写了localhost:1234,并得到此错误。



修正:

  mysql:host = $ hostname_localhost; port = 1234; dbname = $ database_localhost,$ username_localhost,$ password_localhost); 
echo'Connected to DB';
pre>

I have a PHP script which I use to connect to a MySQL database. Connection through mysql_connect works perfectly, but when trying with PDO I get the following error:

SQLSTATE[HY000] [2005] Unknown MySQL server host 'hostname' (3)

the code I use to connect is below:

    <?php  
    ini_set('display_errors', 1);
    error_reporting(E_ALL);

    $hostname_localhost ="hostname";  
    $database_localhost ="dbname";  
    $username_localhost ="user";  
    $password_localhost ="pass";  
    $user = $_GET['user'];  
    $pass = $_GET['pass'];

    try{
        $dbh = new PDO("mysql:host=$hostname_localhost;dbname=$database_localhost",$username_localhost,$password_localhost);
        echo 'Connected to DB';
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $dbh->prepare("SELECT check_user_company(:user,:pass)");
        $stmt = $dbh->bindParam(':user',$user,PDO::PARAM_STR, 16);
        $stmt = $dbh->bindParam(':pass',$pass,PDO::PARAM_STR, 32);

        $stmt->execute();

        $result = $stmt->fetchAll();

        foreach($result as $row)
        {
            echo $row['company_id'].'<br />';
        }

        $dbh = null;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
    } 
    ?>  

Thanks in advance

解决方案

Got the same problem. Mine solution was another database port. I wrote localhost:1234 and got this error.

Fixed with:

mysql:host=$hostname_localhost;port=1234;dbname=$database_localhost",$username_localhost,$password_localhost);
        echo 'Connected to DB';

这篇关于无法使用PDO连接到MySQL服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆