douhezi2285 2012-06-13 15:25
浏览 70

SQLSTATE [HY000] [2002]无法通过套接字'/var/run/mysqld/mysqld.sock'连接到本地MySQL服务器(2)

I've got this error after transferring my website from my computer to a 1and1 server.

I also get the following error, although I am not sure if it is directly related:

Fatal error: Call to a member function prepare() on a non-object in /homepages/37/d153694643/htdocs/sites/mjbox/func/functions.php on line 29

I tested the connection to my 1 and 1 server on a separate file and there are no errors.

Here is most of my 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>';

and some of my functions.php file including line 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' ) ;

    }

This is line 29:

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

What might be causing the connection to fail and the fatal error?

  • 写回答

1条回答 默认 最新

  • dongling4288 2012-06-13 15:59
    关注

    I would honestly go back to a single page, no includes, with the following.

    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]);
    }
    

    It seems to be an issue with the connection string, but this should eliminate that for sure.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器