php 未捕获的异常 'PDOException' 带有消息 'SQLSTATE[HY000]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11384783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]
提问by Tom
I converted the following working code:
我转换了以下工作代码:
<?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');
?>
to PDO:
到 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');
?>
Which gave me
这给了我
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)' in /home/content/58/9508458/html/pabrowser/comsumcompro.php:4 Stack trace: #0 /home/content/58/9508458/html/pabrowser/comsumcompro.php(4): PDO->__construct('mysql:host=;dbn...', NULL, NULL) #1 {main} thrown in /home/content/58/9508458/html/pabrowser/comsumcompro.php on line 4
致命错误:未捕获的异常 '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 中抛出
采纳答案by Tina
Please add
请加
include('config.php');
回答by ts.
You've forgotten include('config.php');
你忘记了 include('config.php');
回答by CodeCaster
Your $db_hostor $db_namevalue are wrong or you are supplying invalid credentials.
您的$db_hostor$db_name值错误或您提供的凭据无效。
回答by Andrey Vorobyev
Your connection string is wrong or the MySQL server is not responding. Use a try ... catchconstruction, like this:
您的连接字符串错误或 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();
}
And if you will see "Connection failed" message, you understand what's wrong.
如果您会看到“连接失败”消息,您就明白出了什么问题。

