0

我一直在寻找解决方案,但似乎没有任何效果。我已添加端口 3307 (web africa) 南非。但我仍然收到此错误

数据加载:
致命错误:未捕获的异常“PDOException”,消息“SQLSTATE [HY000] [2002] 连接超时”在 /home/sea503/public_html/phpsqlinfo_addrow.php:11 堆栈跟踪:0 /home/sea503/public_html/phpsqlinfo_addrow .php(11): PDO->__construct('mysql:host=mysq...', NULL, NULL) 1 {main}在第11行的/home/sea503/public_html/phpsqlinfo_addrow.php
中 抛出

我的代码看起来像这样

<?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="****";


?>
4

2 回答 2

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');
于 2013-05-27T07:15:47.973 回答
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)
                                       ^^^^  ^^^^
于 2013-05-27T07:08:37.593 回答