Uncaught ArgumentCountError: Too few arguments to function getusers::getUsers(), 0

Hello, Here I am trying to query the information from the DB and display it. But it is not working properly.
I get this error:
Fatal error : Uncaught ArgumentCountError: Too few arguments to function getusers::getUsers(), 0 passed in D:\XAMPP\htdocs\magebit_test\test.php on line 5 and exactly 1 expected in D:\XAMPP\htdocs\magebit_test\database2\getusers.php:7 Stack trace: #0 D:\XAMPP\htdocs\magebit_test\test.php(5): getusers->getUsers() #1 {main} thrown in D:\XAMPP\htdocs\magebit_test\database2\getusers.php on line 7

Please tell me how can I solve this issue.

this is getusers.php class

require "dbconnection.php";
require "userData.php";

class getusers extends dbconnection{

public function getUsers($sql){
    $stmt=$this->connect()->prepare($sql);
    $stmt->execute();
    while ($row = $stmt->fetch()) {
        $data[]= new userData($row); 
    }
    if (!empty($data)) {
        return $data;
    }else{
        return null;
    }
}

this is test.php class where I am trying to display my query in table.

<?PHP
 require "database2/getusers.php";
 $users = new getusers();
 ?>

<?php
      $result=$users->getUsers("SELECT * FROM subscribers");
      if ($result) {
        foreach ($result as $data) {
          echo "<tr><td>".$data->getUserId()."</td>";
          echo "<td>".$data->getUserEmail()."</td>";
          echo "<td>".$data->getUserTime()."</td></tr>";
        }
      }
    ?>

In this line
$users = new getusers();
you are not passing your SQL connection variable to the function.
The function you defined is expecting an argument and you are passing in nothing.

1 Like

Personally I like using PDO as you can simply do the following:

class DatabaseObject
{
    static public string $table = "";

    public static function fetch_all(): array
    {
        $sql = "SELECT * FROM " . static::$table;
        return Database::pdo()->query($sql)->fetchAll(PDO::FETCH_OBJ);

    }
}

Then you simply do :slight_smile:

use DatabaseObject as getUsers;

getUsers::$table = "subscribers";

$users == getUsers::fetch_all();
foreach ($users as $user) {
    echo "User email is " . $user->email . "<br>"; // I just did this an example as I don't like storing PHP code inside MySQL
} 

I haven’t tested this out, but I just wanted to show you it using Active Record Design Pattern. This can also be done very easily in mysqli.

1 Like

Whatever your actual code is for test.php (the error doesn’t match the posted code), it has a call to the ->getusers() method without any call-time parameter, which would be the sql statement you are trying to execute.

I am sorry but I did not clearly got this code.

It looks like to me that you aren’t passing in parameters (variables) ,so do query instead of execute. Then you can simply get fetch or fetchAll. I also think what would help you is if you showed us how your database table is structured as just looking at the code it looks to me like you are doing something wrong in that area?

echo "<tr><td>" . $data->getUserId() . "</td>";

Looks like a method call rather that retrieving data as I would expect to see the following instead.

echo "<tr><td>" . $data->getUserId . "</td>";

The following is from my website and gives an example in what I’m talking about:

    foreach ($cms as $record) {
        echo '<article  class="display">' . "\n";
        echo "<h3>" . $record['heading'] . "</h3>\n";
        echo sprintf("<h4> Created by %s on %s updated on %s</h4>", $record['author'], CMS::styleDate($record['date_added']), CMS::styleDate($record['date_updated']));
        echo sprintf("<p>%s</p>\n", nl2br(CMS::intro($record['content'], 200, $record['id'])));
       echo '</article>';
    }
Sponsor our Newsletter | Privacy Policy | Terms of Service