0

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
  • 2
    where is your $con value established? I don't see you connected to the MySQL database, simply passing the variable Commented Aug 3, 2013 at 23:16
  • Pass the $conn as a function argument Commented Aug 3, 2013 at 23:16
  • This isn't about your direct question, but you might want to incorporate strtolower() in your check for 'solo' see php.net/manual/en/function.strtolower.php for more info. Commented Aug 4, 2013 at 1:20

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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:

  1. You need to connect to SERVER
  2. You need to choose the DB u work against
  3. You will execute queries against that DB.

2 is not mandatory.

Comments

0

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.

2 Comments

$con is defined. dont worry about that.
Fatal error: Call to a member function prepare() on a non-object in on $stmnt=$con->prepare($cleansesql);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.