在 PHP 中创建到 PDO 的连接时出错

Error on creating connection to PDO in PHP(在 PHP 中创建到 PDO 的连接时出错)
本文介绍了在 PHP 中创建到 PDO 的连接时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

今天为了迁移到php 5.30,我把最新版的lampp卸载重装了,突然一个很简单的app连接mysql数据库失败.我正在使用 PDO 进行连接,并收到以下错误:

Today, I removed and reinstalled the latest version of lampp in order to move to php 5.30, and suddenly a very simple app is failing to connect to the mysql database. I'm using PDO to connect, and receiving the following error:

Warning: PDO::__construct() [pdo.--construct]: [2002] Invalid argument (trying to connect 
via unix://) in /home/raistlin/www/todoapp/home.php on line 9

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002]
Invalid argument' in /home/raistlin/www/todoapp/home.php:9 Stack trace: #0
/home/raistlin/www/todoapp/home.php(9): PDO->__construct('mysql:host=loca...', 'USER', 
'PASSWORD') #1 {main} thrown in /home/raistlin/www/todoapp/home.php on line 9

为了调试,我目前没有捕捉到错误.

I am not catching the error at the moment, for the sake of debugging it.

以下代码足以在我的系统上复制该问题:

The following code is enough to replicate the issue on my system:

<?php
$DBACCESS = array(
    "connstring"=>"mysql:host=localhost;dbname=todoapp",
    "host"=>"localhost",
    "user"=>"user",
    "password"=>"password",
    "todoapp"=>"todoapp"
    );

    echo implode('<br >',$DBACCESS);

    $dbh = new PDO($DBACCESS['connstring'],$DBACCESS['user'],$DBACCESS['password']);

    $dbh = null;
?>

在网上查找,我发现一两个其他人遇到了同样的问题,但他们都没有收到回复,更不用说有效的回复了.有谁知道发生了什么?我在配置中遗漏了什么吗?我需要做什么来修复它?

Looking online, I've found one or two other people with the same issue, but none of them have received a response, much less a working one. Does anyone know what is happening? Is there something I missed in the configuration? What do I need to do to fix it?

推荐答案

通常意味着你需要指定 TCP/IP (1),或者告诉 MySQL 你的 Unix socket 在哪里 (2):

Usually means that you need to specify TCP/IP (1), or tell MySQL where your Unix socket is (2):

  1. "mysql:host=127.0.0.1" 或 "mysql:host=localhost;port=3306"
  2. "mysql:unix_socket=/var/run/mysqld/mysqld.sock"

这篇关于在 PHP 中创建到 PDO 的连接时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死锁异常代码?)
PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滚动游标不起作用)
PHP PDO ODBC connection(PHP PDO ODBC 连接)
Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔术方法)
php pdo get only one value from mysql; value that equals to variable(php pdo 只从 mysql 获取一个值;等于变量的值)
MSSQL PDO could not find driver(MSSQL PDO 找不到驱动程序)