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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.