Zerpex Posted March 12, 2012 Share Posted March 12, 2012 Hi again everyone! I'm sitting here, developing the same site - on my localhost the site is working great! Then when I upload it, to another testserver (production for developers), I get following: Notice: Undefined variable: db in C:\xampp\htdocs\lucas\sidebar.php on line 3 Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\lucas\inc\functions.php on line 13 I'm wondering why I get it, because if I do a try / catch on the db connection, and do a select * on the content table, it returns results, the paths should be almost the same.. On localhost I run: localhost/lucas/, on the prod-server I run liveip/lucas/ Can any spot the problem? I'm not a big xampp dude myself, I've attached an dump of my PHP info also, just rename the .php to .html Thank you a lot! Lucas R / Zerpex 17762_.php Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2012 Share Posted March 12, 2012 Cannot really help you without seeing the code (less any database credentials) that reproduces the error to narrow down the problem from a half-dozen different possibilities to the one that is causing the problem in your code. Where is $db being defined? Where is it being used? Is there any code in between that is un-setting it? How are you including the files that is different between your development system and the live server? Do you have any header redirects that don't have exit; statements after them? Is your code altering the error_reporting/display_errors settings and is hiding information from where the problem first starts? Is output_buffering on or intentionally being used in the code and it is hiding php error messages? Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326414 Share on other sites More sharing options...
Zerpex Posted March 12, 2012 Author Share Posted March 12, 2012 Hmm, I don't changed the code at all, my db gets defined in inc/db.php, the db.php is included, in the functions.php file, in same folder, using require_once, the functions.php file, is required_once in the header.php, which is in the web-root, the header.php is require_once in the index.php I will give you more info, as soon as I get home! Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326416 Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2012 Share Posted March 12, 2012 When you post your code, show the opening php tags that are in the files, because that is yet another thing YOUR code could be doing that could allow it to work on one server configuration and not another. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326419 Share on other sites More sharing options...
creata.physics Posted March 12, 2012 Share Posted March 12, 2012 On your home server the file is loaded successfully which populates $db, and on the live server $db is undefined and your thrown an error saying prepare fails because it's called off a non-object (undefined $db var). This is obviously a file path issue. Just make sure you correct all links and path settings before you upload to your live server. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326431 Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2012 Share Posted March 12, 2012 If an include path was wrong or an included file wasn't present, there would be at least two warning messages. Since notices and fatal errors are being reported, there's a good chance that warnings are enabled and would have been displayed. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326448 Share on other sites More sharing options...
Zerpex Posted March 12, 2012 Author Share Posted March 12, 2012 Hi again guys, I'm home from work. About the includes, they're included, because if I set another path, I get an error about failed stream, file not found or something like that. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326459 Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2012 Share Posted March 12, 2012 You still haven't posted the code we would need to see to be able to actually help you with what your code is doing that is server/configuration specific. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326481 Share on other sites More sharing options...
Zerpex Posted March 12, 2012 Author Share Posted March 12, 2012 You still haven't posted the code we would need to see to be able to actually help you with what your code is doing that is server/configuration specific. I had pasted the code to pastebin earlier today, just forgot to put it here http://pastebin.com/htg4XWek this is the functions.php file <?php $db_username = 'user'; $db_password = 'pass'; $db = new PDO('mysql:host=127.0.0.1;dbname=lucas;charset=UTF8', $db_username, $db_password); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); ?> Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326485 Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2012 Share Posted March 12, 2012 That's still not all the code needed to reproduce the problem. You are basically asking someone who is not standing right next to you to tell you what your code is doing without seeing your code. If your new PDO is failing, there would be a fatal runtime error and the remainder of the code wouldn't run. Therefore, either your new PDO statement is not being seen as php code or it is not being executed at all or it is and $db is defined at that point. If it is not defined later, then your code is doing something to it. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326565 Share on other sites More sharing options...
Zerpex Posted March 12, 2012 Author Share Posted March 12, 2012 That's still not all the code needed to reproduce the problem. You are basically asking someone who is not standing right next to you to tell you what your code is doing without seeing your code. If your new PDO is failing, there would be a fatal runtime error and the remainder of the code wouldn't run. Therefore, either your new PDO statement is not being seen as php code or it is not being executed at all or it is and $db is defined at that point. If it is not defined later, then your code is doing something to it. I can say so much, if I have index.php, where I have the functions.php included using require_once('inc/functions.php'); etc, and then, right after the include call the function, that is giving the error - and nothing else, so it's only the call to the function I haven't pasted here Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326567 Share on other sites More sharing options...
PFMaBiSmAd Posted March 12, 2012 Share Posted March 12, 2012 You cannot be serious. The error is occuring when sidebar.php is referencing the $db variable, which doesn't exist, when your code on line 3 in sidebar.php is calling the getMenuItems($db) function. Did you even read the first error message? The relevant code is everything from the start of your index.php file through to where you include sidebar.php and then through to at least line 3 in sidebar.php. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326625 Share on other sites More sharing options...
Zerpex Posted March 13, 2012 Author Share Posted March 13, 2012 I've learned, that breaking down the problem, to only those specific places is better, when I take same folder structure, but only have that 1 function called in Index which is Line 13 in the functions.php, I still get the problem. So at this point, no sidebar, or other functions is included except the getMenuItems Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326696 Share on other sites More sharing options...
PFMaBiSmAd Posted March 13, 2012 Share Posted March 13, 2012 You haven't shown your subset of code, either, so no one here can possibly help you with what it is you are still doing wrong in it. Best guess, you are using a URL in your include statements, so either the code making the database connection or the code referencing the $db variable have completely different program scopes. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326699 Share on other sites More sharing options...
Zerpex Posted March 13, 2012 Author Share Posted March 13, 2012 If I use urls in my includes, I should get an error, because it would not be able to find the file. I will link the subset of my code in 1-1.5 hour, I'm on the train to work atm. Anyway, what I first did, was to take the phpinfo from the dev server where it ain't working and the my localhost and prod server, to compare. The big difference is windows on dev server and *nix on the 2 others, the include_path for the windows server also seems weird to me, and then that Virtual Directory Support is enabled on the windows server too. Again, posting the code soon! Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326703 Share on other sites More sharing options...
PFMaBiSmAd Posted March 13, 2012 Share Posted March 13, 2012 The include_path would only matter if you were using a filename syntax in the include statement that would cause php to search the include_path (any thing that doesn't start with a ./ or ../ or is an absolute file system path or a URL) AND you have multiple different connection scripts that are found in the include_path setting that use a variable other than $db. Your question about if a URL would cause an error doesn't answer the guess that was ventured, are you or are you not using a URL in the include statements? Anyway, you wouldn't get an include error by using a URL if the file was in a public folder and the php settings needed to allow a URL to be used in an include statement are set to on. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326707 Share on other sites More sharing options...
Zerpex Posted March 13, 2012 Author Share Posted March 13, 2012 When doing includes I do like on index.php: <?php include_once('inc/functions.php'); ?> If the file structure is like this: index.php inc/functions.php inc/db.php And in functions.php I include like: <?php include_once('db.php'); //functions go after this line ?> Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326709 Share on other sites More sharing options...
PFMaBiSmAd Posted March 13, 2012 Share Posted March 13, 2012 include_once('db.php'); ^^^ That will search the include_path first, then will look in the folder where the main requested file is (index.php), then in the current working directory. It will only check the inc folder if the include_path is not set. If you have more than one db.php file present and the first one found is not setting the $db variable, you can get the symptom you have reported. Edit: I would recommend echoing something unique in the 'correct' db.php file so that you know if it is the one that is actually being included. Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326716 Share on other sites More sharing options...
Zerpex Posted March 13, 2012 Author Share Posted March 13, 2012 Will see If $thisistherightdbvar works, I'm at work in 7 mins Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326718 Share on other sites More sharing options...
Zerpex Posted March 13, 2012 Author Share Posted March 13, 2012 include_once('db.php'); ^^^ That will search the include_path first, then will look in the folder where the main requested file is (index.php), then in the current working directory. It will only check the inc folder if the include_path is not set. If you have more than one db.php file present and the first one found is not setting the $db variable, you can get the symptom you have reported. Edit: I would recommend echoing something unique in the 'correct' db.php file so that you know if it is the one that is actually being included. You saved my day! There was a file called db.php somewhere on the server, which did ruin it totally! I renamed my db.php to db_connect.php Thank you soooo much! Quote Link to comment https://forums.phpfreaks.com/topic/258741-php-returns-undefined-variable/#findComment-1326723 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.