1

我转换了以下工作代码:

<?php
    include('config.php');
    $link = mysql_connect($db_host, $username, $password);
    mysql_select_db($db_name);

    $id= $_POST["uniqi"];
    $comments= $_POST["comments"];
    $comments= mysql_real_escape_string($comments);
    $comments = strip_tags($comments);

    $update = "UPDATE mastertable SET comments = '$comments' WHERE id_pk= '$id'";
    mysql_query($update, $link);
    mysql_close();
    header('Location: ccccc.com/pabrowser/… Updated');
?>

到 PDO:

<?php
    $id= $_POST["uniqi"];
    $comments= $_POST["comments"];
    $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $username, $password);

    $sql = "UPDATE mastertable SET comments=?    WHERE id_pk=?";
    $q = $conn->prepare($sql);
    $q->execute(array($comments, $id));

    header('Location: ccccc.com/pabrowser/… Updated');
?>

这给了我

致命错误:未捕获的异常 'PDOException' 带有消息'SQLSTATE[HY000] [2002] 无法通过 /home/content/ 中的套接字'/var/lib/mysql/mysql.sock' (2)' 连接到本地 MySQL 服务器58/9508458/html/pabrowser/comsumcompro.php:4 堆栈跟踪:#0 /home/content/58/9508458/html/pabrowser/comsumcompro.php(4): PDO->__construct('mysql:host=;dbn ...', NULL, NULL) #1 {main} 在第 4 行的 /home/content/58/9508458/html/pabrowser/comsumcompro.php 中抛出

4

4 回答 4

4

你忘记了include('config.php');

于 2012-07-08T16:39:58.627 回答
3

您的$db_hostor$db_name值错误,或者您提供的凭据无效。

于 2012-07-08T16:38:17.693 回答
1

请加

include('config.php');
于 2012-07-08T16:46:17.333 回答
0

您的连接字符串错误或 MySQL 服务器没有响应。使用try ... catch结构,如下所示:

include('config.php');

$id = $_POST["uniqi"];
$comments = $_POST["comments"];
try {
    $conn = new PDO("mysql:host=$db_host;dbname=$db_name", $username, $password);
}
catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

如果您将看到“连接失败”消息,您就明白出了什么问题。

于 2012-07-08T17:05:52.737 回答