Jump to content

[SOLVED] Function help


Aureole

Recommended Posts

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. :P

Link to comment
https://forums.phpfreaks.com/topic/68334-solved-function-help/
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.