简体   繁体   English

SQLSTATE [HY000] [2002]错误

[英]SQLSTATE[HY000] [2002] error

I am currently trying to insert data from a CSV file into my mysql data base. 我目前正在尝试将CS​​V文件中的数据插入到我的mysql数据库中。 The form and all the functions work properly because I've tested to make sure It's reading file, etc. When I prepare the PDO to insert into mysql I get no error in Chrome. 表格和所有功能都正常工作,因为我已经过测试以确保它正在读取文件等。当我准备将PDO插入到mysql中时,我在Chrome中没有错误。 Now when I let it execute() that's when I get server error in Chrome(any browser) and I looked in my error log file that showed up where function is saying: 现在当我让它execute()时,我在Chrome(任何浏览器)中出现服务器错误,我查看了我的错误日志文件,其中显示了函数所在的位置:

SQLSTATE[HY000] [2002] 
   No connection could be made because the target machine actively refused it.

I'll paste my code on the function that I have made. 我会将我的代码粘贴到我所做的功能上。
Code: 码:

function returnBack(){

    header("Location:csvuploader/csvuploaderform.php?seterror=1");
    exit;

}

if (isset($_POST['submit'])){

    /*******************************CHECK TO MAKE SURE FORM IS PROPERLY FILLED AND GET NAME OF COLUMNS IN CSV FILE *********************************************************/
    if (empty($_FILES['file'] ) )
    {
        returnBack();

    } 
    if (empty($_POST['firstname'])){
        echo "firstname is empty";

        returnBack();
    }
    if (empty($_POST['lastname'])){
        echo "lastname is empty";

        returnBack();
    }if (empty($_POST['email'])){
        echo "email is empty";

        returnBack();
    }
    if (empty($_POST['phone'])){

        returnBack();
    }

    $file = $_FILES['file']['tmp_name'];    

    $handle = fopen($file , "r");

    $fileop = fgetcsv($handle,1000,",");

    $fileop=array_map("strtoupper",array_map("trim",$fileop));

    $firstname_index = array_search(strtoupper($_POST["firstname"]),$fileop);
    if ($firstname_index===false){
        returnBack();
    }
    $lastname_index = array_search(strtoupper($_POST['lastname']),$fileop);
    if ($lastname_index===false){

        returnBack();
    }
    $email_index = array_search(strtoupper($_POST['email']),$fileop);
    if ($email_index===false){

        returnBack();
    }
    $phone_index = array_search(strtoupper($_POST['phone']),$fileop);
    if ($phone_index===false){

        returnBack();
    }
    /***********************ASSIGN COLUMN VALUES TO ACCORDING VARIABLES AND INSERT THEM INTO CSV TABLE IN DB *************************************/
    try {  
  # MySQL with PDO_MYSQL  
  $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);  
  $DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

    }  
    catch(PDOException $e) { 
        echo "I'm sorry, I'm afraid I can't do that.";  
        file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);   
    }
    /*$fileop = fgetcsv($handle,1000,",")*/
    while (($fileop=fgetcsv($handle)) !== false)
    {

        $fileop=array_map("trim",$fileop);

        $firstname = $fileop[$firstname_index];

        $lastname = $fileop[$lastname_index];

        $email = $fileop[$email_index];

        $phone = $fileop[$phone_index];


        $insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')");
        //var_dump($insertdata);

        $insertdata->execute();


    }

}


$DBH = null;

UPDATED ------------------------------------- Heres some info 更新-------------------------------------继承人一些信息

PHP info - PHP Version 5.3.10, System Windows NT W03 6.0 build 6002 (Windows Server 2008 Standard Edition Service Pack 2) i586 PHP信息 - PHP Version 5.3.10, System Windows NT W03 6.0 build 6002 (Windows Server 2008 Standard Edition Service Pack 2) i586

UPDATED ------------------------------------ 更新 - - - - - - - - - - - - - - - - - -

I am able to connect to MySQL server and browse it - insert data from other functions that I have made and they all work perfect. 我能够连接到MySQL服务器并浏览它 - 插入我所做的其他功能的数据,它们都完美无缺。 It is just this one will give me an error. 只是这一个会给我一个错误。

UPDATED ------------------------------------------------------------------------- 更新 - - - - - - - - - - - - - - - - - - - - - - - - - ------------------------

Figured it out guys thanks for all your help. 把它想出来谢谢你的帮助。 I actually needed to fill in all the data columns for it to work. 我实际上需要填写所有数据列才能使用。 For instance I had (firstname,lastname,email,phone) I needed to fill in the username,password,status part. 例如我有(名字,姓氏,电子邮件,电话)我需要填写用户名,密码,状态部分。 Thanks for all your help guys! 谢谢你们的帮助! Hope this will help anyone who falls in the same hole! 希望这会帮助任何陷入同一个洞的人!

Not necessarily part of your issue but you are using prepared statmemnts incorrectly. 不一定是您的问题的一部分,但您正在使用准备好的statmemnts错误。 PArt of the point of using prepared statments is so that the DB takes care of the db specific escaping. 使用准备好的语句的PArt是这样的,DB负责db特定的转义。 In order to do this you write the query with placeholders that then get replaced with the the values passed to the statement. 为此,您使用占位符编写查询,然后将其替换为传递给语句的值。 So in this case: 所以在这种情况下:

$insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES (?,?,?,?)"); 
$insertdata->execute(array($firstname,$lastname,$email,$phone));

Or with named placeholders which is a bit more robust: 或者使用更稳健的命名占位符:

$insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES (:firstname, :lastname, :email, :phone)"); 
$insertdata->execute(array(
  ':firstname' => $firstname,
  ':lastname' => $lastname,
  ':email' => $email,
  ':phone' => $phone
));

Now as far as your error id be willing to bet you are using localhost as the hostname which can cause issues on windows - try using 127.0.0.1 instead. 现在,只要您的错误ID愿意打赌您使用localhost作为主机名,这可能导致Windows上出现问题 - 请尝试使用127.0.0.1 So for example: 例如:

$DBH = new PDO("mysql:host=127.0.0.1;dbname=the_db_name", $username, $password); 

Figured it out guys thanks for all your help. 把它想出来谢谢你的帮助。 I actually needed to fill in all the data columns for it to work. 我实际上需要填写所有数据列才能使用。 For instance I had (firstname,lastname,email,phone) I needed to fill in the username,password,status part. 例如我有(名字,姓氏,电子邮件,电话)我需要填写用户名,密码,状态部分。 Thanks for all your help guys! 谢谢你们的帮助! Hope this will help anyone who falls in the same hole! 希望这会帮助任何陷入同一个洞的人!

while (($fileop=fgetcsv($handle)) !== false)
{

    $fileop=array_map("trim",$fileop);

    $firstname = $fileop[$firstname_index];

    $lastname = $fileop[$lastname_index];

    $email = $fileop[$email_index];

    $phone = $fileop[$phone_index];

    $csvusername = $firstname . $lastname;

    $csvpasswordname = $lastname . $firstname;

    $status = "Open";




    $sth = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone, username, password, status, memberview) VALUES (?,?,?,?,?,?,?,?)"); 
    $sth->execute(array($firstname, $lastname, $email, $phone, $csvusername, $csvpasswordname, $status, $memberview));

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM