0

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??

whispers
  • 962
  • 1
  • 22
  • 48
  • [Reference: What is variable scope, which variables are accessible from where and what are “undefined variable” errors?](https://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Funk Forty Niner Feb 15 '18 at 21:37
  • You will have to pass the connection to the function or the function will never see it because it is not global. – Jay Blanchard Feb 15 '18 at 21:37
  • Wow... look at the time difference between my comment above and the close. I'd call this the closeness award of the day. – Funk Forty Niner Feb 15 '18 at 21:38
  • @deceze if its a duplicate, then post the link to the former question. (otherwise its not very helpful) I have tried to pass $conn as an argument, but it didnt work.. (I was hoping NOT to use globals,.. but doing so worked so far for the $tablename var. – whispers Feb 15 '18 at 21:41
  • 1
    Uhm, yeah… look at the top of your question. – deceze Feb 15 '18 at 21:42
  • @deceze Ahh thank you, I was not aware it would be posted there. Although I didnt want to, I opt'd to use globals. :( (its working, but I feel scummy!) haha I'm still not clear on another way if passing them as arguments to the function do not work? – whispers Feb 15 '18 at 21:53

0 Answers0