problem with functions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wake2010
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2011 10:46 am

problem with functions

Post by wake2010 »

im new to php and studying a course at college on it but i have a little issue which i hope someone can help me with

i have made the following function

Code: Select all

function connectme()
{
	if($mysqlcheck = '0')
	{
	}
	if($mysqlcheck = '1')
	{
	if(!$sqlconnection)
		{ 
 			try
 			{
	 			$mytest = mysql_connect("localhost", "username", "pass");
 				if(!$mytest)
 				{
	 				throw new Exception('MySQL Connection error: ' . mysql_error());
					$servoff = 1;
 				}
				else
 				{
	 			$sqlconnection = true;
				$servoff = 0;
 				}
			}
 			catch (Exception $e)
			{
				$deverrortitle = "SQL Error";
				$devText = "we have this error happening ". $e->getMessage()."\n";
				$fulldevtext = "<h2>" . $devTitle . "</h2><h3>" . $deverrortitle . "</h3><p>" . $devText . "</p>";
				
				
			}
			
		}
	}
}
what i want to do is test a sql connection and see if it is available and if not i want to insert the $deverrortitle, $servoff and $devText in a different php file which calls this one
something like this
db connects display nothing
db fails send $devText,$servoff and $deverrortitle to index.php and display them when i echo them

however when i use the code above and echo them in index.php it does not show them

any ideas
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: problem with functions

Post by Celauran »

You're using assignment operators (=) when you should be using comparison operators (==). Also, $mysqlcheck doesn't exist within the scope of this function. You'll need to pass it in as a function argument or specify that it's a global variable. Your function also doesn't return anything.
wake2010
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2011 10:46 am

Re: problem with functions

Post by wake2010 »

first off thanks for the reply in responce to that, do all functions REQUIRE a return? if so how would i display that return in the main page, second (total noob here) how do i define a global variable?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: problem with functions

Post by Celauran »

wake2010 wrote:do all functions REQUIRE a return?
No. I find it easier for debugging to at least return TRUE/FALSE, but it is not required.
wake2010 wrote:if so how would i display that return in the main page

Code: Select all

$returnValue = some_function_call();
echo $returnValue;
wake2010 wrote:second (total noob here) how do i define a global variable?
Using the "global" keyword.

Code: Select all

function myfunction($something)
{
    global $foo;
    ... etc
wake2010
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2011 10:46 am

Re: problem with functions

Post by wake2010 »

ok one final thing, how to i send something to the function:
i know in VB it is

Code: Select all

functionName(1)
and then

Code: Select all

functionName(a as int) 
would i be right in thinking this using the function above:

Code: Select all

in config.php
function connectme($mysqlcheck='')
{
// php code
     if($mysqlcheck =='0')
     { 
      //php code
     }
}

in index.php
include 'config.php';
//use checksql to see if i wantto use sql or bypass it for testing
$checksql = '1';
connectme($checksql);
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: problem with functions

Post by Celauran »

Yes, that's right.

Code: Select all

function functionName($variable = NULL)
passes $variable into the function or sets it to NULL if it is not specified. In this case, functionName($foo) and functionName() would both be valid function calls.
wake2010
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2011 10:46 am

Re: problem with functions

Post by wake2010 »

cheers worked 100% you just saved me hours of anger and fustration =]
wake2010
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2011 10:46 am

Re: problem with functions

Post by wake2010 »

ok all i have a new problem with this function.
for the reccord the last one worked but since then i realised that my college blocks anything with the word game so i built a all in one site for both my testing server and production server if you need more code just let me know and i will add it
for now i have done this

Code: Select all


//inside config.php
global $servoff,$sqlun,$sqlpass; 

$sqlconnection  = false;
$servoff = null;
$deverrortitle = null;
$devText = null;
$sqlun = null;
$sqlpass = null;
global $sql, $sql_db;
$servername = $_SERVER['SERVER_NAME'];
switch($servername)
{
case 'serv1.net':

$sql = mysql_connect("localhost","username1", "pass1");
$sql_db = mysql_select_db("db1");
break;
case 'serv2.com':
global $sqlun, $sqlpass;
$sql = mysql_connect("localhost", "username2", "pass2");
$sql_db = mysql_select_db("advancet_college");
break;
}

function connectme($mysqlcheck)
{
	global $sqlconnection;
	if($mysqlcheck == 0)
	{
	}
	if($mysqlcheck == 1)
	{
	if(!$sqlconnection)
		{ 
 			try
 			{
				global  $servoff;
				echo $sqlun;
	 			$mytest = $sql;
 				if(!$mytest)
 				{
	 				throw new Exception('MySQL Connection error: '.mysql_error());
					$servoff = 1;
 				}
				else
 				{
					
	 			$sqlconnection = true;
				$servoff = null;
 				}
			}
 			catch (Exception $e)
			{
				global $developer;
				global $devText, $deverrortitle;
				
				global $devText, $deverrortitle;
				$deverrortitle = "SQL Error";
				$devText = "we have this error happening ". $e->getMessage();
				
				
				
			}
			
		}
	}
	
}

Code: Select all

//inside index.php
require 'includes/config.php';
connectme($mysqlcheck);

if ($sqlconnection == true)
{
	connectdb($servername);
}
if ($servoff == 1)
{
echo "Server is set to ".$servoff;
}
however the $servoff is found in index.php when im coding but when i run php it does not dispay $servoff and it doesnt send $sqlconnection...
few hours in with this problem and still no luck any ideas??

thanks :D in advance to all who reply =]=]

also how do i make it so i can do this

Code: Select all

my_sql_connect("localhost",$db_username,$dp_password);
?? i have tried from what my tutor said

Code: Select all

my_sql_connect("localhost",'$db_username','$db_password');
but it fails (im sure its me messing up at 3am but its when i get the time
when
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: problem with functions

Post by Celauran »

There are a number of issues here. First, your connectme function call

Code: Select all

connectme($mysqlcheck);
$mysqlcheck hasn't been defined anywhere, so you're essentially calling connectme(NULL), which clearly won't work.

Second, you're using the global keyword all over the place, which isn't a great idea. It can be handy at times, but it can also come back to bite you in the ass easily enough. Whenever possible, you'd do better to add arguments to the function signature and pass variables in that way.

While on the topic of variables, there are a number of variables you define, sometimes also define as global, but never assign any values. $sqlun, $sqlpass, $developer are all examples.

Code: Select all

if ($sqlconnection == true)
{
    connectdb($servername);
}
I don't see the function connectdb() defined anywhere.

What is it you're trying to do, exactly?
wake2010
Forum Newbie
Posts: 12
Joined: Tue Nov 01, 2011 10:46 am

Re: problem with functions

Post by wake2010 »

i have 2 sites which will run this php script,
both have different URLs and therefor have different login details for sql

what i want to do is this:

1. Create a function that collects the data from $_SERVER['SERVER_NAME'] and compares it to a switch to see if it has that server URL in the switch.
2. If found i want to collect the data needed to connect to that servers SQL server, using $vars to make it easy to manipulate if needed
3. run mysql_connect("localhost",$var1,$var2) and if it connects to set a $_SESSION['serveronline'] to 1 else it is 0.
4. if it is true i want it to try to connect to the servers database $db_name and attampt to connect to that if passes sets the serveronline session to 0 else it is a 2
5. if the sql checking fails receive an email telling me so
reason for the serveronline session keys is that based on the output i can post the exact error on the index.php file ie

serveronline=0 "server online"
serveronline =1 or 2 "Server Offline we will be back shortly. Our technicians are aware of this issue"

EMAIL
if serveronline =1 email "SQL failed to connect to sql server"
if serveronline =2 email "sql can not find DB"
i have just read about using sessions instead of global which i am starting to do and sofar works (have not included sql yet though)
Post Reply