简体   繁体   English

我正在尝试使用 php 连接到 mysql 数据库,但它给了我一个错误

[英]I'm trying to connect to mysql database with php, but it gives me an error

I'm using Jetbrains and Mysql to work on this practical project, but when I connect to the mysql database it gives me the following error:我正在使用 Jetbrains 和 Mysql 来处理这个实际项目,但是当我连接到 mysql 数据库时,它给了我以下错误:

C:\wamp64\bin\php\php5.6.40\php.exe C:\wamp64\www\Social_Network\Includes\connection.php
Connection failed: SQLSTATE[HY000] [1049] Unknown database 'social_network'
Process finished with exit code 0

I made sure several times that the database name is the same name and there are no spelling errors at all (I copy pasted it from the database)我多次确保数据库名称是相同的名称并且根本没有拼写错误(我从数据库中复制粘贴它)

Here's my code:这是我的代码:

  <?php


$servername = "localhost";
$username = "root";
$password = "";

try {
    $conn = new PDO("mysql:host=$servername;dbname=social_network", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}


//?>

Welll, there's little that can be done about it.好吧,对此无能为力。 MySQL thinks that the database does not exist . MySQL 认为数据库不存在

  • is the server the correct one?服务器是正确的吗?
  • is the case sensitivity set correctly?区分大小写是否正确设置? "Social_Network" and "social_network" might be considered different. “Social_Network”和“social_network”可能被认为是不同的。
  • can you access the database with those parameters using a different tool (eg HeidiSQL, SQLYog, SQLterm, in a pinch even phpMyAdmin)?您可以使用不同的工具(例如 HeidiSQL、SQLYog、SQLterm,甚至是 phpMyAdmin)使用这些参数访问数据库吗?

Actually, JetBrains PHPStorm has a SQL terminal utility that can diagnose the connection.实际上,JetBrains PHPStorm 有一个 SQL 终端实用程序可以诊断连接。 You may want to use it (once it knows what database you're connecting to, it will also warn you of several possible errors such as using the wrong table name or column name).您可能想要使用它(一旦它知道您要连接的数据库,它还会警告您几个可能的错误,例如使用错误的表名或列名)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在PHP中连接到MySQL数据库时出错 - Error trying to connect to MySQL database in PHP 我正在尝试使用 PHP 连接到 mysql 数据库,但它不起作用 - I'm trying to connect to a mysql database using PHP but it won't work 我正在尝试使用php和mysql OOP向数据库添加一条记录 - I'm trying to add a record to the database with php and mysql OOP SQLite没有给我这样的列错误,列是我要存储到表中的变量的值 - SQLite gives me a no such column error, with the column being the value of a variable I'm trying to store into the table 我正在尝试在magento的产品页面上显示面包屑,但这给了我一个致命错误 - I'm trying to display breadcrumbs on product page in magento but it gives me a fatal error 使用PHP时,尝试从MySQL数据库中选择特定查询可以代替一切 - With PHP, trying to select a specific query from a MySQL database gives me everything instead 我目前正在尝试用PHP连接MySQL数据库 - I am currently trying to connect MySQL database in PHP PDO ODBC MYSQL:我尝试连接,但它给了我一个致命错误 - PDO ODBC MYSQL : i try to connect and it just gives me a fatal error mysql ERROR 2003 [HY000] - 当我尝试连接到远程 mysql 时 - mysql ERROR 2003 [HY000] - When i'm trying to connect to remote mysql 尝试从PHP连接数据库MySQL(不成功) - Trying to connect database MySQL from PHP (Not Successful)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM