无法使用php PDO连接到我的数据库

时间:2015-01-14 14:59:48

标签: php mysql csv pdo

我正在尝试为网站创建一个函数,在您登录后,可以将csv文件上传到数据库csv表中。但是当我尝试上传csv文件时,它会给我以下错误;

SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'server2.bhosted.nl' (111)

我使用的代码是 - >

<?php 
session_start();
// mysql hostname
$hostname = 'server2.bhosted.nl';
// mysql username
$username = 'username';
// mysql password
$password = 'password';
// databasename
$dbname = 'birdss';
// Get Extension of the File.
$extension= end(explode(".", basename($_FILES['file']['name'])));
// isset Determines if a variable is set and is not NULL. Set Size Limit less then 10 MB=10485760 bytes. Extension must be CSV.
if (isset($_FILES['file']) && $_FILES['file']['size'] < 10485760 && $extension== 'csv')
{  
// We will get csv file and save it in a $file
$file = $_FILES['file']['tmp_name']; 
//$handle is a valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen(). fopen() used to open file.
$handle = fopen($file, "r"); 
    // We will use try{} Catch() statements here. 
    try { 
    // Database Connection using PDO
    $dbh = new PDO("mysql:host=$hostname;dbname=$dbname;", $username, $password);
    // Truncate Table before insertion
    $STMR = $dbh->prepare("TRUNCATE TABLE csv");
    $STMR->execute();
    // Prepare the statement for the insertion in the table
    $STM = $dbh->prepare('INSERT INTO csv (tijd, aantal) VALUES ("'.$data['tijd'].'", "'.$data['aantal'].'")');
        //Check handel is True or False
        if ($handle !== FALSE) 
        {
            // fgets() Gets a line from file pointer and read the first line from $handle and ignore it.   
            fgets($handle);
            // While loop used here and  fgetcsv() parses the line it reads for fields in CSV format and returns an array containing the fields read. 
            while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
            {
            // For Executing prepared statement we will use below function
            $STM->execute($data);
            }       
            //The file pointed to by handle will be closed.
            fclose($handle);

            // Closing MySQL database connection
            $dbh = null; 
            // If data inserted successfully we will redirect this page to index.php and show success message there with code 77083368
            header( "location:index.php?ServerStatsAdded=77083368"); 


        }

    }
    //  
    catch(PDOException $e)
    {
    die($e->getMessage());
    }


}
else 
{
// Error mesage id File type is not CSV or File Size is greater then 10MB.
header( "location:index.php?ServerStatsAdded=37767");
}

&GT;

请帮助我!!

1 个答案:

答案 0 :(得分:0)

通过在$ dbname变量之后删除第二个;来尝试这种方式,但再次检查您的凭据。

 $hostname = 'server2.bhosted.nl';
 $username = 'username';
 $password = 'password';
 $dbname = 'birdss';   
 $dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);

修改

As error (111) means only listen on localhost, If you have lines like this :

 skip-networking
 bind-address = 127.0.0.1

my.cnf 配置文件中,您应该对它们进行注释(在行的开头添加#),然后重新启动MySQL。