Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible PHP scope issue with file include?
    primarykey
    data
    text
    <p>I'm currently re-writing one of my old PHP scripts so that the code is well structured for future updates.</p> <p>One thing I'm trying to do, is create a configurations file - so that settings can be easily changed by editing one single file.</p> <p>source code of file: functions.php</p> <pre><code>function ConnectToDB() { //database configurations... $host = "localhost"; $dbuser = "root"; $dbpass = ""; $tblname = "parents"; $connection = mysql_connect($host,$dbuser,$dbpass); if (!$connection) { echo 'Could not connect to the database. Please contact the administrator for more information.'; } // select the db $db_selected = mysql_select_db($tblname, $connection); if (!$db_selected) { echo 'Could not select the parents evening table. Please contact the administrator for more information.'; return false; }else{ return true; } } </code></pre> <p>file: config.php</p> <pre><code>&lt;?php //database configurations... $host = "localhost"; $dbuser = "root"; $dbpass = ""; $tblname = "parents"; ?&gt; </code></pre> <p>This is my new code: file: functions.php</p> <pre><code>function ConnectToDB() { include('inc/config.php'); global $host; //needed becuase the scope of the variable throws an error otherwise. global $dbuser; //needed becuase the scope of the variable throws an error otherwise. global $dbpass; //needed becuase the scope of the variable throws an error otherwise. global $tblname; //needed becuase the scope of the variable throws an error otherwise. $connection = mysql_connect($host,$dbuser,$dbpass); if (!$connection) { echo 'Could not connect to the database. Please contact the administrator for more information.'; } // select the db $db_selected = mysql_select_db($tblname, $connection); if (!$db_selected) { echo 'Could not select the parents evening table. Please contact the administrator for more information.'; return false; }else{ return true; } } </code></pre> <p>My old code used to work perfectly, however now I've changed variables around to different files, my application keeps outputting "Could not select the parents evening table. Please contact the administrator for more information." - which means my application is not connecting to the database properly.</p> <p>Does anyone have any ideas what's wrong with my code? I thought it was a scope issue at first, however I just can't find out what I've done wrong here.</p> <p>Thanks in advance for any replies.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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