dean7 Posted September 21, 2010 Share Posted September 21, 2010 Hi all, Ive had alot of problem with one script latly but I carnt see what it accually causing the problems. My script is giving me an error saying: Notice: Undefined variable: carin in /home/www/*****.com/test/test.php on line 23 And: Notice: Trying to get property of non-object in /home/www/*****.com/test/test.php on line 23 My Code: $carin1 = mysql_query ("SELECT carid FROM users WHERE username='$username'") or die (mysql_error()); $carin = mysql_fetch_object($carin1);// Use function to make sure there in a car..function carcheck(){if ($carin->carid == ''){ // LINE 23echo ("You must be in a car before doing any races.");}else{echo ("You are currently in: $carin->carid");}exit();} // End function Where its saying ive got an undefined variable carin I accually havent as it is defined. :S Anyone see what is wrong with that part of my script?. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/213988-undefined-variable-even-when-there-isnt/ Share on other sites More sharing options...
the182guy Posted September 21, 2010 Share Posted September 21, 2010 The function has no access to that variable unless you global it. Add this to the function: global $carin; See here for info about variable scope. Quote Link to comment https://forums.phpfreaks.com/topic/213988-undefined-variable-even-when-there-isnt/#findComment-1113631 Share on other sites More sharing options...
dean7 Posted September 21, 2010 Author Share Posted September 21, 2010 The function has no access to that variable unless you global it. Add this to the function: global $carin; See here for info about variable scope. Where about to I add this to my function? Quote Link to comment https://forums.phpfreaks.com/topic/213988-undefined-variable-even-when-there-isnt/#findComment-1113632 Share on other sites More sharing options...
taquitosensei Posted September 21, 2010 Share Posted September 21, 2010 right at the beginning. Is is supposed to $carin or is it $carin1? Quote Link to comment https://forums.phpfreaks.com/topic/213988-undefined-variable-even-when-there-isnt/#findComment-1113633 Share on other sites More sharing options...
rwwd Posted September 21, 2010 Share Posted September 21, 2010 to clarify, either:- function carcheck(){global $carin;if ($carin->carid == ''){ // LINE 23 or, pass it as a parameter into the function function carcheck($carin){if ($carin->carid == ''){ // LINE 23 Simple! Rw Quote Link to comment https://forums.phpfreaks.com/topic/213988-undefined-variable-even-when-there-isnt/#findComment-1113638 Share on other sites More sharing options...
KevinM1 Posted September 21, 2010 Share Posted September 21, 2010 Don't use 'global'. Pass the value into the function through its argument list. Quote Link to comment https://forums.phpfreaks.com/topic/213988-undefined-variable-even-when-there-isnt/#findComment-1113643 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.