php PDO没有与数据库的连接

时间:2018-04-17 23:32:00

标签: php mysql pdo

我决定创建一个注册网页并将其与db连接,但我遇到了问题:

  

错误:SQLSTATE [HY000] [1049] 未知数据库'site'致命错误:   未捕获错误:在null中调用成员函数query()   C:\ xampp \ htdocs \ sm \ application \ lib \ PDOMain.class.php:20堆栈跟踪:   #0 C:\ xampp \ htdocs \ sm \ application \ lib \ User.class.php(11):PDOMain-> selectPDO('sign_up','*','WHERE login ='ad ...')# 1   C:\ xampp \ htdocs \ sm \ home.php(4):User-> getUserWhereLogin('admin')#2   {main}抛出C:\ xampp \ htdocs \ sm \ application \ lib \ PDOMain.class.php   第20行

我已经做了一段时间了,任何帮助都会受到赞赏。

我有PDOMain:

class PDOMain
{
    private $MPDO;

    function __construct($dbname = "site", $host ="localhost", $user ="root", $password ="")
    {
        try {
            $this->MPDO = new PDO ("mysql:dbname={$dbname};host={$host}", "{$user}", "{$password}");
            return $this->MPDO;
        } catch (PDOException $e) {
            echo "ERROR: " . $e->getMessage();
        }
    }

    function selectPDO($db, $cols, $where = "", $order = "", $limit = "")
    {
        $sql = "SELECT{$cols} FROM `{$db}` {$where} {$order} {$limit}";
        $rs = $this->MPDO->query($sql);
        $rs->execute();
        if ($where != "") {
            $row = $rs->fetchAll(PDO::FETCH_ASSOC);
            return $row;
        } else {
            $row = $rs->fetchAll(PDO::FETCH_ASSOC);
            return $row;
        }
    }
function insertPDO($db,$cols="",$values="")
{
if ($cols=="") echo "ERROR! </<br>";
$sql="INSERT INTO `{$db}`({$cols}) VALUES ({$values})";
$this->MPDO->query($sql);
}
function updatePDO($db,$what,$val,$where)
{
    $sql="UPDATE `{$db}` SET {$what}='{$val}' {$where}";
    $this->MPDO->query($sql);
}
function deletePDO($db,$where){
        $sql= "DELETE FROM `{$db}` {$where}";
        return $this->MPDO->query($sql);
}
}

我有php类用户:

require_once("PDOMain.class.php");
class User extends PDOMain{
    function __construct($table)
    {
        parent::__construct();
        $this->table=$table;
    }

    function getUserWhereLogin($login){
        $user=$this->selectPDO($this->table,"*","WHERE login='$login'");
        if(count($user)>0) return $user;
        else return 0;
    }
}

我在home.php中使用它:

require_once("application/lib/User.class.php");
$userObject = new User("sign_up");
$user=$userObject->getUserWhereLogin("admin");
print_r($user);

Here is a screenshot showing the schema of my table

0 个答案:

没有答案