数据已加载:<br/> <b>致命错误</b>:带有消息的未捕获异常“PDOException”

时间:2013-05-27 07:02:43

标签: php database google-maps

我已经搜索了解决方案,但似乎没有任何工作。我已添加端口3307(web africa)南非。但我仍然收到此错误

  

已加载数据:
致命错误:未捕获的异常   'PDOException',消息'SQLSTATE [HY000] [2002]连接时间   out in in /home/sea503/public_html/phpsqlinfo_addrow.php:11堆栈追踪:   0 /home/sea503/public_html/phpsqlinfo_addrow.php(11):   PDO-&gt; __ construct('mysql:host = mysq ...',NULL,NULL)1 {main}
  抛出 /home/sea503/public_html/phpsqlinfo_addrow.php   第 11

我的代码看起来像这样

<?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
$dbh = new PDO("mysql:host=mysql.spri.co.za;port=3307;dbname=kruger_park_live",$sean_sql,$########);
$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; 
?>

和dbinfo.php一样

<?php
 $host= "****;port=3307"; $username="****"; $password="****"; $database="****"; $dbname="****";


?>

2 个答案:

答案 0 :(得分:0)

你在做什么

$dbh = new PDO("mysql:host=...;dbname=...,$s..,$P..);
      // I removed the actual values for your security

但你的变量是

$host= ""; 
$username="..."; 
$password="...";
$database="...";

我相信你想做

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

设置变量,但将值用作变量名称

在错误消息中查看此行时很明显 -

PDO->__construct('mysql:host=mysq...', NULL, NULL)
                                       ^^^^  ^^^^

答案 1 :(得分:0)

我已将""改为单''

$dbh = new PDO("mysql:host=myhost;port=xxxx;dbname=db_name",$sean_sql,$########);

$dbh = new PDO('mysql:host=myhost;port=xxxx;dbname=db_name','$username','$password');