Aureole Posted September 7, 2007 Share Posted September 7, 2007 Hey, I've just started playing around with Functions but I can't get this to work I'm just wondering if anyone knows why. It looks fine to me, thanks a lot. <?php session_start(); include('functions.php'); dbConnect(); echo('Hello '.$_SESSION['mem_dname'].'. You have '); checkPms($_SESSION['mem_id']); echo(' New Messages.'); ?> My functions.php file: <?php function dbConnect() { $dbhost = 'localhost'; $dbuser = '********'; $dbpass = '********'; $dbname = '********'; $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); } function checkPms($who) { $query = "SELECT * FROM `messages` WHERE pmread='0' AND to_id='{$who}'"; $newpms = mysql_num_rows($query); return $newpms; } ?> Just so you know the Database is connected and I have populated the Database and it's just showing..."You have New Messages". EDIT: Yes I did have session_start(); in my script I just forgot to put it on here. Link to comment https://forums.phpfreaks.com/topic/68334-solved-function-help/ Share on other sites More sharing options...
ToonMariner Posted September 7, 2007 Share Posted September 7, 2007 yep its variable scope... this is not reall the best use of a function your databse connection is something that should (in 99%) of cases only do once per page and as such does NOT need to be in a function. read up on variable scope - we could tell you but you won't learn it anywhere near as well... http://uk2.php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/68334-solved-function-help/#findComment-343587 Share on other sites More sharing options...
Aureole Posted September 7, 2007 Author Share Posted September 7, 2007 But the thing is I use that function dbConnect(); on every page and it does connect as I have other things that query the Database etc. this is the only one that won't work...or does the checkPms(); need to not be a Function too...? I read the variable scope thing but and tried making $newpms global etc. and that didn't work...so I don't know... Link to comment https://forums.phpfreaks.com/topic/68334-solved-function-help/#findComment-343593 Share on other sites More sharing options...
ToonMariner Posted September 7, 2007 Share Posted September 7, 2007 the go and replace dbConnect(); with something like require_once('path/to/dbconnect/file.php'); and put all the db connection stuff in that one file. (No functions mind!) Link to comment https://forums.phpfreaks.com/topic/68334-solved-function-help/#findComment-343595 Share on other sites More sharing options...
Aureole Posted September 7, 2007 Author Share Posted September 7, 2007 Ok but what I'm trying to say is that dbConnect(); works fine, regardless of if I shouldn't have it in a Function. The thing that won't work is checkPms(); although I got it to work now...but thanks anyway. I'll go do that. Link to comment https://forums.phpfreaks.com/topic/68334-solved-function-help/#findComment-343599 Share on other sites More sharing options...
ToonMariner Posted September 7, 2007 Share Posted September 7, 2007 since you have declared the connection INSIDE A FUNCTION the resource WILL ONLY BE AVAILABLE INSIDE THAT FUNCTION. Link to comment https://forums.phpfreaks.com/topic/68334-solved-function-help/#findComment-343603 Share on other sites More sharing options...
Aureole Posted September 7, 2007 Author Share Posted September 7, 2007 Ok I think I grasp the idea of it now, thanks a lot for your help. Link to comment https://forums.phpfreaks.com/topic/68334-solved-function-help/#findComment-343610 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.