No I get it. We all started somewhere. What I would suggest is learning the basics first before trying to dive into something as complex as what you’re attempting to do. You’re going to do a lot of damage to your current code base if you don’t know what it’s really doing.
As for your “mysqli going to PDO” comment, I don’t think this is a PDO vs mysqli issue. You could easily do the same thing in mysqli. Not many people on the forums here can switch between mysqli and PDO easily. The issue you’re having is with understanding how the language actually works. I’ll help you out with 2 examples (1 for PDO and 1 for mysqli) here using your original code.
MySQLi version
<?php
try {
$db = new mysqli('localhost', 'root', '', 'test');
} catch(Exception $ex) {
die('Unable to connect to the database');
}
/* Bad Apple value is determined by failed logins, registrations and the like */
function badapple(object $db, int $user_sid, string $ba_action): int {
$sql = 'SELECT count, action FROM badapple WHERE user_sid = ? AND action = ?';
$prepare = $db->prepare($sql);
$prepare->bind_param('is', $user_sid, $ba_action);
$prepare->execute();
$prepare->store_result();
if ($prepare->num_rows > 0) {
$prepare->bind_result($count, $action);
while ($prepare->fetch()) {
$baValue = $count;
}
} else {
/* No record for this user/action combo. */
$baValue = 0;
}
return $baValue;
}
print badapple($db, 1, 'ban') . "\r\n";
PDO version
<?php
try {
$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');
} catch(Exception $ex) {
die('Unable to connect to the database');
}
/* Bad Apple value is determined by failed logins, registrations and the like */
function badapple(object $db, int $user_sid, string $ba_action): int {
$sql = 'SELECT count, action FROM badapple WHERE user_sid = :user_sid AND action = :ba_action';
$prepare = $db->prepare($sql);
$parameters = [
':user_sid' => $user_sid,
':ba_action' => $ba_action,
];
$prepare->execute($parameters);
if ($prepare->rowCount() > 0) {
while ($row = $prepare->fetch()) {
$baValue = $row['count'];
}
} else {
/* No record for this user/action combo. */
$baValue = 0;
}
return $baValue;
}
print badapple($db, 1, 'ban') . "\r\n";
Both gives me the same exact outcome. The issue you’re having is understanding how to “pass in” your database connection. If the database connection is returning a string and not an object, that most likely means you’re having a database connection issue whether that’d be a typo in your connection string or the database truly is unreachable. You should really be putting that in a try-catch
block so if it truly is a database issue, you don’t want to continue. The reason why you would do this is because if you don’t have access to the database and you’re trying to pull data from the database, there’s nothing for you to pull from because the connection to the database was never made in the first place. You want to halt this action immediately because whatever you do next such as pulling data from the database, inserting data into the database, updating a row from the database, or even deleting a row from the database cannot be performed due to the connection not being there.
So if your $db
variable is returning a string or a boolean, you would really want to halt this immediately so that you can debug why this is happening. The reason why these 2 examples “work” is because of what I said earlier about “scope”. We provide the function with what it needs such as database connection and the other 2 parameters. So when we pass in our database connection into the function, now the function’s scope has the database connection.
In simple terms, you can think of a function’s scope as “local” to that function. Nothing outside of that function has any idea on what’s going on inside of that function. You can think of this as something happening on Mars. Yes, we have telescopes (this could be considered a “global” item - you typically don’t want to use this since it’s a no-no in the programming world) that can reach Mars and what not, but we don’t know what’s going on on the surface of Mars. We can only see the red clouds moving, we don’t know what’s really happening inside of those red clouds. This is the same thing with functions. Each function doesn’t have any scope into one another and that’s ok. That’s how it should work.
Hopefully that helps you a little bit.