0

I recently converted a code block into a function so I can call it easily more than just once. My problem is that as soon as I related the block to a function it fails the SQL query every time. Here's my code block:

    function checkEvent()
{
if(!empty($_GET['e']))
    {
        $sql = mysqli_query($link, "SELECT * FROM events WHERE EventID = '" . mysqli_real_escape_string($link, $_GET['e']) . "'");

        if($sql && mysqli_num_rows($sql)==1)
            {
                if($row = mysqli_fetch_row($sql))
                    {
                        $eventid = $row[0];
                        $eventname = $row[1];
                        $desc = $row[2];
                        $time = $row[3];
                        echo $_GET['e'];
                    }
            }               
        else
            {
                echo $sql;
                $failure = "Num Rows Error encountered: " . mysqli_error($link) . " / Num Rows: " . mysqli_num_rows($sqlE);
            }
    }
}

Now, I've added echos in the relevant places to check and where it currently says echo $sql; if I change that to echo "Fail."; then it will indeed do that. I have tried to get the result as a number of rows and that comes back blank. I don't understand this as my EventID is an AUTO INCREMENT and as such HAD to start at 1. I've triple checked the first entry is 1 as well.

I'm probably not seeing something really obvious, I just can't understand why this code block stopped working the instant I placed a function block around it.

4
  • Where do you declare "$link"? I would suspect that's the culprit. Commented Jun 14, 2013 at 23:47
  • variable scope issue? where is your connection $link defined? try adding it as a function parameter function checkEvent($link) or as global $link inside the function or if in a class $this-link. Commented Jun 14, 2013 at 23:47
  • $link is declared inside my base.php which contains all my db connection details. It is included at the top of the document that this function is within. As I said it worked perfectly before-hand so surely the $link variable is passed still? It's been declared in the base.php which is the very first thing in this file to be called. Commented Jun 15, 2013 at 0:03
  • Answered my own stupid damn question. Thanks guys, really appreciate this and learned something I won't soon forget! :) Commented Jun 15, 2013 at 0:14

1 Answer 1

1

$link doesn't exist inside your function. You either need to pass that as a parameter to the function or skip it entirely and use the "current" DB connection.

"current" in quotes because while it should work just fine while you're utilising a single database connection for the entire process, as soon as you'd start using multiple connections (to connect to multiple databases,) this approach would fail terribly.

Sign up to request clarification or add additional context in comments.

Comments

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.