I made this function. It's actually one of the first functions I've ever made. However, I can't get it to execute. $con is defined, i just didn't paste it. In fact, all of the variables are defined.
function cleanse() {
$cleansesql = "Select * FROM rated_teams WHERE server='$server' AND name='$myteam' AND opposition='$opposer'";
$result = mysqli_query($con, $cleansesql)
or die('A error occured: '.mysqli_error());
while (($row = mysqli_fetch_array($result))) {
if ($row['server'] == $server && $row['name'] == $myteam && $row['opposition'] == $opposer && $row['myscore'] == $myscore && $row['oscore'] == $oscore && $row['location'] == $location) {
echo "There is a Match.";
} else {
echo "There are no matches";
}
}
}
And this is how I'm calling it.
if ($solo == "solo" || $solo == "Solo" || $solo == "SOLO") {
echo $solo." <br />";
if (!empty($myscore)) {
echo $myscore." <br />";
if (!empty($oscore)) {
echo $oscore." <br />";
if (!empty($location)) {
echo $location." <br />";
cleanse();
}
}
}
}
Maybe I'm not calling it correctly. I just need someone who knows more than me to help... that'll be most of you hahaha.
3 Answers 3
Pass the information to your cleanse function, like $con (given that you've created your connection to the database prior to calling this function), $server, $myteam and $opposer so it can work with it.
So the definition of your function would become:
function cleanse($con, $server, $myteam, $opposer) { ... }
And you'd call it this way:
cleanse($con, $server, $myteam, $opposer);
Comments
Notice that you are using a variable $con which is the connection to MySQL.
You have not created a connection to MySQL prior to the query.
I suggest reading the basic PHP manuals on using mysqli.
Remeber the follwing:
- You need to connect to SERVER
- You need to choose the DB u work against
- You will execute queries against that DB.
2 is not mandatory.
Comments
Campari is right - that is why your function is not working, however:
You are duplicating your code. The function does not need to check if there is a match between your test criteria and the output of the sql because the database does that for you. What your sql says is "fetch all the results that match all of my criteria" then your function says "check all the results match my criteria" this is slow and not required. If you write your sql properly, there is rarely any need for further checking or merging of data in PHP.
function cleanse($connection, $server, $myteam, $opposer)
{
$cleansesql="Select * FROM rated_teams WHERE server=? AND name=? AND opposition=?";
$stmnt=$connection->prepare($cleansesql);
$stmnt->bind_param("sss",$server,$myteam,$opposer);
$stmnt->execute();
$stmnt->store_result();
return $stmnt->num_rows;
}
This (untested) function I've written for you should return the number of rows in rated_teams that match all of the criteria in your variables. It is also safe to input user-entered data as it uses prepared statements and is thus not susceptible to SQL injection.
$connas a function argument