I have a main.php file.
In this file, I include my PDO/DB connection, like so:
Content of PDO include file:
$dbhost = 'blah';
$dbname = 'blah';
$dbuser = 'blah'; //Read/Write
$dbpass = 'blah';
$conn = new PDO('mysql:host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpass);
included in the main file like so: (I also declare the table name after it)
include '../_include/path_to_pdo.php';
$tablename = 'some_table_name';
I have another file I am including that is basically just a file full of functions to keep the main file more readable, and not have all the database stuff in there and only use the predetermined functions.
include 'external_functions.php';
In this external_functions.php file.. are a group of functions that will return/output data (or markup..whatever) in the MAIN.php file when executed.
These functions make database calls...etc..
However, these functions in the external_functions.php file are NOT seeing the:
$tablename
$conn
vars there were declared in the MAIN.php file.. or in the PDO connection/setup...
In the external_functions.php file.. if I echo out:
$tablename
It displays fine..
but not when used inside the function... nor the $conn database handle var declared in the PDO include file.
I have even tried to pass in the $tablename as an argument to the function..
example of the function:
function get_someList($tablename){ //doesnt work, var not seen
//get some list
$someDetails_sql = "SELECT * FROM $tablename ORDER BY category"; //doesnt work
$someDetails_stmt = $conn->prepare($someDetails_sql);
$someDetails_stmt->execute();
$someDetails_stmt->setFetchMode(PDO::FETCH_ASSOC);
$_someList = $someDetails_stmt->fetchAll(); //returns multi-dimensional array (and correct count)
//do whatever
}
How can I fix this? and what am I doing wrong? If outside the function, $tablename echo's fine... inside the function its lost? (even when attempting to pas it in as an argument? Why???
Also, how would I go about passing in the $conn var to the function as well, so that it too can be used in the query/prepare statement??