这个错误是什么意思

时间:2013-05-27 13:19:16

标签: php google-maps

我的网站上有地图,您可以在此地图上添加位置和信息。但是在保存和关闭时我得到了这个错误

Data Loaded: <br />
<b>Fatal error</b>:  Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2005] Unknown MySQL server host '$host' (25)' in /home/sea503/public_html/phpsqlinfo_addrow.php:11
Stack trace:
#0 /home/sea503/public_html/phpsqlinfo_addrow.php(11): PDO-&gt;__construct('mysql:host=$hos...', '**************', '************')
#1 {main}
  thrown in <b>/home/sea503/public_html/phpsqlinfo_addrow.php</b> on line <b>11</b><br />

我的代码看起来像这样

    <?php
require("dbinfo.php");// database connections
// Get parameters from URL
$lat = $_GET["lat"];
$lng = $_GET["lng"];
$name = $_GET["name"];
$time = $_GET["time"];
$description = $_GET["description"];
$sighting = $_GET["sighting"];
//Connect to database
try {
$dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);
} catch (PDOException $e) {
 echo 'error :' .$e->getMessage();
}
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
try {
    // Prepare INSERT statement new sigting
        $stmt3 = $dbh->prepare("INSERT INTO tbl_maps (ID, map_client_name, client_time, client_lat, client_lng, client_description, client_sighting)  VALUES (NULL, ?, ?, ?, ?, ?, ?)");
        // Assign parameters
        $stmt3->bindParam(1,$name);
        $stmt3->bindParam(2,$time);
        $stmt3->bindParam(3,$lat);
        $stmt3->bindParam(4,$lng);
        $stmt3->bindParam(5,$description);
        $stmt3->bindParam(6,$sighting);
        $stmt3->execute();
        $data[] = array("name"=>$name, "lat"=>round($lat,6), "lng"=>round($lng,6),"time"=>$time,"description"=>$description,"sighting"=>$sighting);
        //Data for success
        echo json_encode($data);
}
catch(PDOException $e) {
    echo "Error Message.". $e->getMessage() ;// Remove or modify after testing 
    file_put_contents('PDOErrors.txt',date('[Y-m-d H:i:s]').", phpsqlinfo_addrow.php, ". $e->getMessage()."\r\n", FILE_APPEND);  
}
//Close the connection
$dbh = null; 
?>

需要30秒左右,我最终会收到错误消息。请有人帮我解决这个问题

然后dbinfo是

 $host="41.185.13.51;port=3307"; $database="kruger_park_live"; $username="sean_sql"; $password="******";

4 个答案:

答案 0 :(得分:1)

使用双引号而不是单引号,或者$host作为值,

$dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);

如果您的主机地址中有端口,请使用

$dbh = new PDO("mysql:host=$host;port=$port;dbname=$database",$username,$password);

阅读 Strings 了解更多信息。

答案 1 :(得分:1)

您已设置errormode并对错误消息使用exception。您没有捕获异常,因此您将收到此错误。

要发现错误,您将使用trycatch功能。

代码就是这样:

try {
$dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
 echo 'error :' .$e->getMessage();
}

这样您就不会收到错误Uncaught exception 'PDOException' ....

问题出在你的数据库连接中,当你在字符串中使用变量时,你应该使用double qoutes。

更改:

$dbh = new PDO('mysql:host=$host;dbname=$database',$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

收件人(编辑

try {
$dbh = new PDO("mysql:host=$host;port=$port;dbname=$database",$username,$password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
 echo 'error :' .$e->getMessage();
}

有关异常的详细信息,请参阅此处:http://php.net/manual/en/language.exceptions.php

同时检查所有变量以确保它是正确的。您使用的服务器是否可以访问另一台服务器上的端口3307?你确定3307端口?因为默认情况下它是3306

答案 2 :(得分:0)

您的数据库凭据不正确。

请注意,在以下行中:

$dbh = new PDO('mysql:host=$host;dbname=$database',$username,$password);

您使用了单引号而不是双引号“ 当你想在字符串中使用php变量时,你应该使用双引号。 您可以通过两种方式修复此行。

A)

$dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);

B)

$dbh = new PDO('mysql:host='.$host.';dbname='.$database,$username,$password);

答案 3 :(得分:0)

使用双引号而不是单引号。

$dbh = new PDO("mysql:host=$host;dbname=$database",$username,$password);