【问题标题】:SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)SQLSTATE[HY000] [2002] 无法通过套接字 '/var/run/mysqld/mysqld.sock' 连接到本地 MySQL 服务器 (2)
【发布时间】:2012-06-13 15:25:18
【问题描述】:

在将我的网站从我的计算机传输到 1and1 服务器后,我遇到了这个错误。

我也收到以下错误,虽然我不确定它是否直接相关:

致命错误:在非对象上调用成员函数 prepare() /homepages/37/d153694643/htdocs/sites/mjbox/func/functions.php 上线 29

我在单独的文件中测试了与我的 1 和 1 服务器的连接,没有错误。

这是我的大部分 index.php

//Check if the user is logged in
loggedin();

//Check if the submit button has been clicked first
if ( isset( $_POST['submit'] ) ){

    //Check if user exists on database
    match_login($_POST['username'],$_POST['password']);
}

// Retrieve all active posts order by lastest first
$resultarray = retrieve_active_posts();

echo '<div id="content-wrap">';
foreach($resultarray AS $value){
    $filename = substr($value['img_file_name'],9);
    $cat_id = $value['cat_id'];
    echo '<article class="post">';
    echo '<div class="post_title">' . $value['post_title'] . '</div>';
    echo '<div class="post_info">' . 
    'Category: ' . $cat_name = get_cat_name($cat_id) .'<br />'. 
    'Year: ' . $value['post_year'] .'<br />'. 
    $value['post_desc'] .'<br />'. 
    '</div>';
    echo '<div class="link-to-post"><a href="#">Click to view</a></div>';
    echo '<a href="#'.$value['post_id'].'" class="linktopost"><img class="post-thumb" src="img/thumb_/'.$filename.'" alt="MJbox Michael Jackson memorabilia thumbnail" data-postid="'.$value['post_id'].'"/></a>';
    echo '<a href="#'.$value['post_id'].'" class="linktopost"><img class="cover-img" src="img/post-bg-1.png" alt="test" data-postid="'.$value['post_id'].'"/></a>';
    echo '</article>';

}
echo '</div>';

还有我的一些functions.php文件,包括第29行:

//Check if user is logged in
    function loggedin(){
        //Check if the loggedin status is set to true, meaning that user is logged in.
        if ( isset ( $_SESSION['loggedin'] ) && $_SESSION['loggedin'] == true  ) {
            return true;
        }else{

            return false;
        }

    }

    //Check users login details
    function match_login($username, $password){
        //If the button has been clicked get the variables
        //test the connection
            try{
                //connect to the database
                $dbh = new PDO("mysql:host=correct;dbname=correct","correct", "correct");
            //if there is an error catch it here
            } catch( PDOException $e ) {
                //display the error
                echo $e->getMessage();

            }
        //select any username and password that match 
        $stmt = $dbh->prepare("SELECT * FROM mjbox_users WHERE username=? AND password=?");
        $stmt->bindParam(1, $username);
        $stmt->bindParam(2, $password);

        //execute the select statement, put in if statement to provide error if false!?
        if( $stmt->execute() ){
            //count how many rows are found
            $numrows = $stmt->rowCount();
            //if there is a match continue
            if( $numrows > 0 ){
                $_SESSION['loggedin'] = true;
                $_SESSION['username'] = $username;
                header( 'Location: index.php' ) ;
                echo 'yes';
            }
        }
        $dbh = null;
    }

    //logout
    function logout(){
        $_SESSION = array();    
        session_destroy();
        header( 'Location: index.php' ) ;

    }

这是第 29 行:

$stmt = $dbh->prepare("SELECT * FROM mjbox_users WHERE username=? AND password=?");

什么可能导致连接失败和致命错误?

【问题讨论】:

  • 你试过用少量的东西进行测试吗?即一个简单的连接?用这么多代码调试很难吗?我以前见过 cant connect sock 错误,通常是权限错误。即数据库用户无法在主机上连接?或者mysql服务宕机了
  • Double-Triple 检查连接数据是否正确以及您正在连接到正确的服务器(在某些服务器上,数据库不在localhost 下,但它可能在mysql5 下等)。您确定您在外部服务器上的应用没有尝试连接到您本地计算机上的数据库吗?
  • 您在哪一行收到“无法通过套接字连接”错误?当您指定主机名而不是 UNIX 套接字时,您会收到该错误,这很奇怪。第二个错误是第一个错误的结果,$dbh 不是有效的数据库连接,因此您不能调用$dbh-&gt;prepare()
  • 是的,我设置了一个基本的 php 文件,只使用新的 pdo 进行 try 和 catch 并且没有错误

标签: php mysql pdo database-connection fatal-error


【解决方案1】:

老实说,我会返回一个页面,不包含以下内容。

try {
    //connect to the database
    $dbh = new PDO("mysql:host=correct;dbname=correct", "correct", "correct");
    //if there is an error catch it here
} catch( PDOException $e ) {
    //display the error
    echo $e->getMessage();

}
$result = $dbh->query("show tables");
while ($row = $result->fetch(PDO::FETCH_NUM)) {
    print_r($row[0]);
}

这似乎是连接字符串的问题,但这应该可以肯定地消除它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多