PHP的PDO语句错误 [英] php pdo statement error

查看:64
本文介绍了PHP的PDO语句错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php pdo的新手.在这里,我试图使用准备好的语句从数据库中获取数据库记录.但是它没有获取记录.我收到以下错误

I'm a newbie to php pdo. Here i'm trying to fetch my database records from database using prepared statements. But it didn't fetch the records. I'm getting this following error

致命错误:带有消息的未捕获异常'PDOException' 'SQLSTATE [3D000]:无效的目录名称:1046未选择数据库

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[3D000]: Invalid catalog name: 1046 No database selected

为什么我会收到此错误?为什么不从数据库中获取记录?

why i'm getting this error? why it didn't fetch the records from database?

<?php
$user = "root";
$password = "password";

    try {
        $conn = new PDO('mysql:host=localhost;database=evouchers', $user, $password);
        $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    } catch(PDOException $e){
        echo 'DATABASE ERROR : ' . $e->getMessage();
    }

    $sql = "SELECT UserName FROM ebusers ORDER BY UserName";
    $db = $conn->query($sql);
    $db->setFetchMode(PDO::FETCH_ASSOC);

    while($row = $db->fetch())
    {
        print_r($row);
    }
?>

在数据库连接字符串中的

推荐答案

而不是数据库中使用dbname

in your db connection string instead of database use dbname

  $conn = new PDO('mysql:host=localhost;database=evouchers', $user, $password);
  ---------------------------------------^



 $conn = new PDO('mysql:host=localhost;dbname=evouchers', $user, $password);

文档链接: http://php.net/manual/en/pdo.connections .php

这篇关于PHP的PDO语句错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆