Providing Interoperability with MySQL in a PHP Function

I have code with the following form:

<?php
function doSomething{
  //Do stuff with MySQL
  $con->tralalala();
}
$con = connectToDatabase;//This would actually be a line or two.
doSomething();
?>

This (type) code does not work because doSomething () does not have a database connection. Can someone explain why not? I am creating a $ con connection before I call doSomething (). So why does a function act as if there is no connection?

Is there any way to fix this, except for passing the connection to a function like doSomething ($ con)?

+3
source share
1 answer

you will probably need to say what it will look like in the global area:


     function doSomething()
     {
         global $con;
         $con->tralalala();
     }
+5
source

All Articles