/    Sign up×
Community /Pin to ProfileBookmark

PDO Fatal error: Uncaught ArgumentCountError:

I am using wamp and have the following files:
Logic.php containing a Logic class which includes Data.php and creates new Data class object
`$data = new Data;` This is giving the Uncught ArgumentCountError
Data.php containing Data class which extends Db class and requires Config.php
Config.php contains Db class that connects to database
In Db class I have

[code]
class Db extends PDO
{
private $host;
private $dbName;
private $user;
private $pass;
private $charset;

public function Connect()
{
$this->host = “localhost”;
$this->user = “root”;
$this->pass = “”;
$this->dbName = “dbName”;
$this->charset = “utf8mb4”;

try
{
$dsn = “mysql:host=”.$this->host.”;dbName=”.$this->dbName.”;charset=”.$this->charset;
$pdo = new PDO($dsn, $user, $pwd, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
return $pdo;
}
catch (PDOException $e)
{
echo ‘Error: ‘.$e->getMessage();
}
}
}
[/code]

In the Data class I have

[code]
require ‘Config.php’;
class Data extends Db
{
public function FunctionName()
{
try
{
if ($_SERVER[“REQUEST_METHOD”] == “POST”)
{
$email = test_input($_POST[“email”]);
}

if (!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$emailErr = “Invalid email format”;
}

$sql = “Insert statement…..”;

$stmt= $pdo->prepare($sql);
$stmt->execute(values…..);
if($stmt)
{
return true;
}
}
catch (PDOException $e)
{
echo ‘Error: ‘ . $e->getMessage();
}
}
}
[/code]

If I put this PDO construct into the Data class

[code]
public function __construct()
{
$pdo = new PDO($dsn,$user,$pass,array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
[/code]

I get Notice: Undefined variable: for dsn, user and pass.
Any help appreciated.

to post a comment

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 29.2021 — @rpjd1#1639975 Please use this forum's ... tags around your code blocks, rather than using the </> option in the edit window -- which unfortunately does not work nearly as well. I edited your post accordingly.

I'll take a look at your question as time permits. ;)
Copy linkTweet thisAlerts:
@rpjd1authorNov 29.2021 — @NogDog#1639976 cheers, I completely forgot about those tags.
Copy linkTweet thisAlerts:
@NogDogNov 29.2021 — If it's extending Db, then you could probably avoid that error by simply calling your connect() method (and assigning it to a class variable so you can access it in any other method):
<i>
</i>$this-&gt;pdo = $this-&gt;connect();

Note, however, this sounds like it might open the door to having multiple database connections in one script. I might be more inclined to have your config file create one Db object, then passing that in to the constructor or methods of anything else that needs it.
<i>
</i>class Data {
private $pdo;
public function __construct(PDO $pdo) {
$this-&gt;pdo = $pdo;
}
public example_method() {
// use $this-&gt;pdo where needed.
}
}
×

Success!

Help @rpjd1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...