Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

sql - this php code works in localhost but not on server?

we are making an collage project that is about to store website name , category , and details so we are done with website and now we want to make app that will communicate with server through API . we had simple code like this in php script when data is added into database echo json_encode(true) . as android programmers know , retrofit library use key value pair type mechanism so we updated code with following.

<?php

    $websiteName = $_POST['website_name'];
    $websiteCategory = $_POST['website_cat'];
    $websiteDetails = $_POST['website_details'];

    try {
        $pdo = new PDO('mysql:host=localhost;dbname=website' , 'root' , '');
        $sql = 'INSERT INTO website_data SET website_name = :website_name , website_cat = :website_cat , website_details = :web_del';

        $stmt = $pdo->prepare($sql);
        $stmt->bindValue(':website_name' ,$websiteName);
        $stmt->bindValue(':website_cat' ,$websiteCategory);
        $stmt->bindValue(':web_del' ,$websiteDetails);
        $stmt->execute();

        echo json_encode(['response' => 'true']);
    } catch (PDOException $e) {
        $msg = $e->getMessage();
        echo json_encode(['response' => $msg]);
    }
?>

and here is javascript code that performs operation with ajax

function sendData(websiteName , categoryName , websiteDetails){
    var params = 'website_name='+websiteName+'&website_cat='+categoryName+'&website_details='+websiteDetails;
    var xml = new XMLHttpRequest();
    xml.onreadystatechange = function(){
        if(this.status == 200 && this.readyState == 4){
            var response = this.responseText;
            var responseJson = JSON.parse(response);
            console.log('resonse from server' , responseJson['response']);
            if(responseJson['response'] == 'true'){
                addToCurrent(websiteName , categoryName , websiteDetails);
            }else{
                alert('unfortunatley data could not added succesfully');
            }
        } else{
            console.log('there is some problem with sever');
        }
    }
    xml.open('POST' , '../php/addNewWebsite.php' , true);
    xml.setRequestHeader('Content-type' , 'application/x-www-form-urlencoded');
    xml.send(params);
}

this is working as indented in localhost good but it is not working in our free server it shows the alert('unfortunatley data could not added succesfully');i do not know why does this happening. we have free server and domain from the awardspace.com

UPDATE

here is the error message i am getting from the server SQLSTATE[HY000] [2002] No such file or directory Thank You .

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have encountered similiar issues in the past. You should try changing localhost to 127.0.0.1, assuming your MySQL server is running on the same box.

My belief is that the issue that you are encountering is that "localhost" uses a UNIX socket and can not find the database in the standard directory. However "127.0.0.1" uses TCP , which essentially means it runs through the "local internet" on the machine, being much more reliable than a UNIX socket.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...