PHP - 一些错误,不知道如何解决它们

时间:2017-06-19 12:23:26

标签: mysql database class pdo

我得到了不同的错误,我甚至不知道如何修复它们并使其工作,我使用SPL加载器加载我的类,我需要帮助才能修复这些错误!

这是我的数据库类:

$(document).ready(function() {
    var table = $('#example').DataTable( {
        scrollY:        "300px",
        scrollX:        true,
        scrollCollapse: true,
        paging:         false,
        fixedColumns:   {
            leftColumns: 0,
            rightColumns: 1
        }
    } );
} );

这是我的用户类:

<?php

class Database{

  private $pdo_conn;
  private $dsn, $user, $pass;

  // connect to db
  public function __construct($username = "root", $password = "", $host = "localhost", $dbname = "test"){

    $this->dsn = 'mysql:host='. $host . ';dbname=' . $dbname . ';charset=utf8';

    $this->user = $username;
    $this->pass = $password;

    $this->Start();

  }

  public static function Start(){
    try{
      $this->pdo_conn = new PDO($this->dsn, $this->user, $this->pass);
    } catch(PDOException $e) {
      print_r($e);
      exit();
    }
  }

  //Insert, Delete and Create
  public static function RunQuery($sql, $inputs = null){
    $pdo = $this->pdo_conn; //PDO connection.

    if(is_null($inputs)){ //If there are no any inputs (input array)
      $stmt = $pdo->query($sql); //returning a result set as a PDO object.
    } else {
      try{
        $stmt = $pdo->prepare($sql); //Prepares the statment

        if($stmt){ //If there is no error
          $stmt->execute($inputs); //Pass the input array to the statment exacute.
        } else {
          print_r("Doesn't work!");
        }
      } catch(PDOException $e){
        print_r($e); //Print errors array.
        exit();
      }
    }

    return $stmt;

  }

这是错误:

<?php

class Users{

  public function GetUser($id){
    $sql = "SELECT * FROM guests WHERE id = :id";

    $inputs = array(':id' => $id);

    $stmt = Database::RunQuery($sql, $inputs);

    $rowCount = $stmt->rowCount();

    if($rowCount > 0)
      return $stmt->fetchAll(PDO::FETCH_ASSOC);
    else //If there is any data in Database (row available)
        return $rowCount;
  }

}

 ?>

请帮帮我,非常感谢!

0 个答案:

没有答案