Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are trying to access a variable that was created in the global scope from within a function.</p> <p>Either pass the <code>$conn</code> variable in as an argument to the function (this is the prefered option), or use the <a href="http://php.net/manual/en/language.variables.scope.php" rel="nofollow"><code>global</code> keyword</a> to import it into the function's scope.</p> <p>So you could do: <em>(prefered option)</em></p> <pre><code>function validate_details ($conn, $user, $pass) { // function code goes here } // Call the function like this $result = validate_details($conn, 'myuser', 'mypass'); </code></pre> <p>...or this... <em>(not so good)</em></p> <pre><code>function validate_details ($user, $pass) { global $conn; // function code goes here } </code></pre> <p>There are many reasons it is better to pass the connection object in as a parameter, but here are a couple:</p> <ul> <li>It means you can use your function with more that one connection in the same script</li> <li>It means you can easily recycle the function, unmodified, in other scripts</li> <li>If at some point you need to rename the <code>$conn</code> variable in the global scope, you don't have to worry about changing it in the function</li> </ul> <p>I suppose it's also worth mentioning the <a href="http://php.net/manual/en/reserved.variables.globals.php" rel="nofollow"><code>$GLOBALS</code></a> array - this is a <a href="http://www.php.net/manual/en/language.variables.superglobals.php" rel="nofollow">superglobal</a> (i.e. it is available everywhere) that you could use instead, but in practice this is not much different from the <code>global</code> keyword and should generally be avoided for the same reasons.</p> <p>You could use it like this:</p> <pre><code>function validate_details ($user, $pass) { // ... $result = $GLOBALS['conn']-&gt;query($sql); // ... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload