正在尝试使用没有第二个参数的函数


Trying to use function without second parameter

我一直试图在不声明第二个参数的情况下使用一个函数,但它似乎不起作用。我做错了什么?

我还尝试在不使用第二个参数$db->DoQuery($query, null);的情况下声明函数,得到了相同的结果。

<?php 
include($directory . "classes/database.php");
$db = new database;
$db->initiate();
include("../includes/core.php");
$query = "SELECT * FROM services";
$db->DoQuery($query, null);
$num = $db->fetchAll();
print_r($num);
?>

下面是数据库类。

<?php
class database {
  public function initiate() {
    $user             = "user";
    $password         = "pass";
    $hostname         = "localhost";
    $dbn              = "dbt";
    try
    {
      $this->database = new PDO("mysql:host={$hostname};dbname={$dbn}", $user, $password);
    }
    catch(PDOException $e)
    { 
      $error = "I'm unable to connect to the database server.";
      die("Failed to connect to database: " . $e->getMessage());
    }
    $this->database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
  public function DoQuery($query, $query_params) {
    try
    {
      $this->result = $this->database->prepare($query);
      if ($query_params != null)
      {
        $this->result->execute($query_params);
      }
      else
      {
        $this->result->execute();
      }
    }
    catch(PDOException $e)
    {
      die();
    }
  }
  // A function to fetch a single row from the query
  public function fetch() {
    return $this->result->fetch();
  }
  // A function to fetch multiple rows from the query
  public function fetchAll() {
    return $this->result->fetchAll();
  }
    // A function to fetch number of rows from the query
  public function RowCount() {
    return $this->result->RowCount();
  }
}
?>

数据库类的更改

public function DoQuery($query, $query_params = null) {

没有第二个参数的呼叫

$db->DoQuery($query);

使用第二个参数调用

$db->DoQuery($query, null);

将第二个参数设置为array()

public function DoQuery($query, $query_params = array()) {
    try
    {
      $this->result = $this->database->prepare($query);
      $this->result->execute($query_params);
    }
    catch(PDOException $e)
    {
      die();
    }
}

然后,如果您不想传递任何查询参数,那么只需省略第二个参数。

$db->DoQuery($query);