简体   繁体   English

错误:SQLSTATE[HY000] [1049] 未知数据库

[英]Error: SQLSTATE[HY000] [1049] Unknown database

I've been working on some beginner php exercise recently and have encountered an error to which I have no idea what is causing it.我最近一直在做一些初学者的 php 练习,但遇到了一个错误,我不知道是什么导致了它。

It says它说

"Error: SQLSTATE[HY000] [1049] Unknown database 'otkrica'" “错误:SQLSTATE[HY000] [1049] 未知数据库 'otkrica'”

The sql file is correctly named "otkrica" and my db.php looks like this: sql 文件正确命名为“otkrica”,我的 db.php 如下所示:

<?php 
$dsn = "mysql:host=127.0.0.1;charset=utf8;dbname=otkrica";

try{

$pdo = new PDO($dsn,"root","");
  }catch(PDOException $e){
    die("Error: " . $e->getMessage());
  }
?>

I would really appreciate input on this one.我真的很感激对此的投入。 What am I missing?我错过了什么?

The correct answer is found in the comments (credit to cmnardi).正确答案在评论中找到(归功于 cmnardi)。 Posted as a clear answer for anyone looking:发布为任何人的明确答案:

Set the port you are using like: mysql:host=localhost;port=3307;dbname=testdb

I fix this problem wait this way我解决了这个问题 等待这样

$connect = new PDO($dsn, $user, $pass);
$connect->exec("SET character_set_connection = 'utf8'");
$connect->exec("SET NAMES 'UTF8'");

<?php
    $server="localhost";
    $user="root";
    $pass="";
    $dbname="clicksite";
    
    $dsn="mysql:host=$server;dbname=$dbname";
    try{
    $connect=new PDO($dsn,$user,$pass);
    $connect->exec("SET character_set_connection = 'utf8'");
    $connect->exec("SET NAMES 'UTF8'");
    }catch(PDOException $error) {
    
        echo "unable to connect".$error->getMessage();
    
    }
?>

attention: use as wampserver ver2.5 for build database in the server after run wampserver, click left on the icon in notify bar and then click on phpmyadmin and create the database.注意:运行wampserver后作为wampserver ver2.5在服务器上建库,点击通知栏图标左边,然后点击phpmyadmin创建数据库。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM