Jump to content
Sign in to follow this  
BrainPill

Fatal error: Uncaught ArgumentCountError // using execute() in a class

Recommended Posts

I have a challenge to solve regarding PDO and executing prepared statements inside a class.

The way I made the script now is giving an error as output, though the outcome required is okay.

The error type is:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function fruit_stock::get_fruit(), 0 passed in

Could it be the error is caused because PHP requires that the execute() part should be defined or something like that?
I really dont get what to do with it or with $stmt . .

This are the scripts and the table so you can test it yourself.


I'm curious how this is done.

database table : 

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Oct 04, 2018 at 08:42 AM
-- Server version: 5.7.19
-- PHP Version: 7.1.9

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `test_fruit`
--

-- --------------------------------------------------------

--
-- Table structure for table `fruit`
--

DROP TABLE IF EXISTS `fruit`;
CREATE TABLE IF NOT EXISTS `fruit` (
  `rec_nr` int(2) NOT NULL AUTO_INCREMENT,
  `fruit_type` varchar(50) DEFAULT NULL,
  `amount` int(3) NOT NULL,
  `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `changed_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`rec_nr`,`created_on`,`changed_on`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `fruit`
--

INSERT INTO `fruit` (`rec_nr`, `fruit_type`, `amount`, `created_on`, `changed_on`) VALUES
(1, 'apple', 5, '2018-10-04 06:01:48', '2018-10-04 06:01:48'),
(2, 'strawberry', 20, '2018-10-04 06:01:48', '2018-10-04 06:01:48'),
(2, 'banana', 15, '2018-10-04 06:02:54', '2018-10-04 06:02:54'),
(4, 'grapes', 9, '2018-10-04 06:02:54', '2018-10-04 06:02:54');
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Script 1:


<?php 

// run class 
 
 
$amount = '20' ;

include 'fruit-class1.php';
$tel_wrg = new fruit_stock('$amount'); 


$tel_wrg -> get_fruit($amount);

var_dump($tel_wrg);

echo $tel_wrg -> get_fruit();
?>

Script 2 :


<?php 

		// test classes
		
		class fruit_stock {
		 
			
			var $amount;
			
			
		 public $get_fruit;
		  
	 
	
		public function get_fruit($amount ) {
			 
			 
		 
	  var_dump($amount);
	var_dump($this);
	  
	
$servername = "your host";
$username = "your name";
$password = "your password";
$dbname = "test_fruit";
 
 $conn = new mysqli($servername, $username, $password, $dbname);
 
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

 // PREPARED 

  if ($stmt = $conn->prepare("SELECT fruit_type FROM fruit WHERE amount = ? ; " )) { 
 

 
	$stmt->bind_param('i', $amount); //  
 
    $stmt->execute();
   
	$stmt->bind_result($fruit_type);
 
	 
	 
	 
	while ($stmt->fetch()) {
		
	  var_dump($fruit_type);
	  
		  
		  
		 
		 
	} 
}
 
} 
		
		 
	}

 

Share this post


Link to post
Share on other sites

That error message has nothing to do with the database or PDO.  Read the error message, what is it telling you?

Share this post


Link to post
Share on other sites

to give in more variables.

I dont understand, because only $amount is input.

I got rid of the errors with adding $fruit_type to script 1 , but I need to understand for what reason php requires this?

Share this post


Link to post
Share on other sites

You have a method called get_fruit, and you defined it so that it requires one argument.  Then you try to call it without passing an argument.  That's an error.  PHP requires you to pass an argument because you explicitly told it that that method requires an argument.

Share this post


Link to post
Share on other sites

I get it. This is the solution.

$tel_wrg -> get_fruit($amount, $fruit_type);

var_dump($tel_wrg);

echo $tel_wrg -> get_fruit($amount, $fruit_type);

 

Share this post


Link to post
Share on other sites

But now I have another question.

If I want to echo the $fruit_type , or make any kind of condition , outside the function but inside the class what should I do then ?

 

Edited by BrainPill

Share this post


Link to post
Share on other sites

If the class only represents one kind of fruit, then you need to save that value as a class property so that you can access it from other methods in the class.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×