简体   繁体   English

PHP PDO驱动程序

[英]PHP PDO drivers

I just start learning about PDO, I need some help, I Installed PHPStorm, and I just start using it too, I already have a datbase on phpMyAdmin, I made this code, but it gives me an error 我刚刚开始学习PDO,我需要一些帮助,我安装了PHPStorm,我也开始使用它,我已经在phpMyAdmin上安装了datbase,我编写了这段代码,但是它给我一个错误

    <?php
try {
    $handler = new PDO ('mysql:localost;dbname=Database', 'root', 'password');
    $handler -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOExeption $e)
{
    die('Sorry, Database problem');
}
$query = $handler->query('select * from users');
while($r= $query->fetch())
{
    echo $r['name'];
}
?>

here is the error: 这是错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected' in C:\\Users\\user1\\PhpstormProjects\\PDO\\pdo.php:11 Stack trace: #0 C:\\Users\\user1\\PhpstormProjects\\PDO\\pdo.php(11): PDO->query('select * from u...') #1 {main} thrown in C:\\Users\\user1\\PhpstormProjects\\PDO\\pdo.php on line 11. Any help? 致命错误:C:\\ Users \\ user1 \\ PhpstormProjects \\ PDO \\ pdo.php:11中未捕获的异常'PDOException',消息为'SQLSTATE [3D000]:无效的目录名称:1046未选择数据库'堆栈跟踪:#0 C:\\ Users \\ user1 \\ PhpstormProjects \\ PDO \\ pdo.php(11):PDO-> query('select * from u ...')#1 {main}放入C:\\ Users \\ user1 \\ PhpstormProjects \\ PDO \\ pdo。第11行上的php。有什么帮助吗?

thanks in advance :). 提前致谢 :)。

Please make sure your database actually exists and that there are no spelling mistakes in your connection code. 请确保您的数据库确实存在,并且连接代码中没有拼写错误。

Edit 1 编辑1

I also noticed this issue with your connection I'm not sure if this is what is causing it but you need mysql:host= not just mysql:localhost . 我还注意到与您的连接有关的问题,我不确定这是由什么引起的,但您需要mysql:host=而不是mysql:localhost Also you have a spelling mistake localost . 你也有一个拼写错误localost

Change this, 改变这个

$handler = new PDO ('mysql:localost;dbname=Database', 'root', 'password');

To, 至,

$handler = new PDO('mysql:host=localhost;dbname=myDb', $username, $password);

replace the handler declaration for this: 为此,请替换处理程序声明:

$handler = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);

where myDatabase is the one you will be using as default.. 其中myDatabase是您将默认使用的数据库。

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

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