LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-19-2007, 06:48 AM   #1
jayakrishnan
Member
 
Registered: Feb 2002
Location: India
Distribution: Slacky 12.1, XP
Posts: 991

Rep: Reputation: 30
Question calling a function from within another function in php


Code:
db_connect.php

<?php

function connect_db()
{
		global $link;

		$link = mysql_connect("localhost", "root","protocol");

		mysql_select_db("claims",$link);

		return $link;
}

?>

claim_no.php

<?php

function claimno()
{
	global $claim_no;

	$link = mysql_connect("localhost", "root","protocol");

	mysql_select_db("claims",$link);

	$sql = "SELECT max(claim_no) as claim_no FROM claim_details;";

	$result = @mysql_query($sql, $link) ;

	while ($row = mysql_fetch_array($result))
	{
		$f1=$row['claim_no'];
	}

	if($f1==0)
	{
		$claim_no=1;
	}
	else
	{
		//print "Max claim_no : ".$f1->value."<br>";


			$claim_no=$f1;

		$claim_no=$claim_no+1;

		//echo "New shgid =".$shgid;
	}
}

?>
I would like to replace

Code:
	$link = mysql_connect("localhost", "root","protocol");

	mysql_select_db("claims",$link);
in claim_no.php with this code

Code:
	include 'db_connect.php';

	connect_db();
But it seems to be not working as the value of link from db_connect.php is not passed onto claim_no.php

In short how do i call a php function from within another function
 
Old 06-19-2007, 09:15 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,159

Rep: Reputation: 258Reputation: 258Reputation: 258
Hi

The conncect_db function returns the link, so you need to use:
$link = connect_db();

Alternatively you can add "global $link;" in the claimno function.
 
Old 06-19-2007, 09:36 AM   #3
jayakrishnan
Member
 
Registered: Feb 2002
Location: India
Distribution: Slacky 12.1, XP
Posts: 991

Original Poster
Rep: Reputation: 30
Hey thanks, for the reply, i just added the global keyword, and it worked
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question for calling function harrylee2003 Programming 3 08-06-2006 08:04 AM
calling a function from a pointer spx2 Programming 3 05-25-2006 05:52 PM
calling function in kernel vishalbutte Programming 0 02-14-2006 08:34 AM
Calling another function from a function using GTK geminigal Programming 4 07-11-2005 04:15 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 01:08 PM


All times are GMT -5. The time now is 12:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration