Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
| Search | Today's Posts | Mark Forums Read
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 27th, 2005, 02:39 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default passing variable to function

hi,

Been struggling with a problem for hours..... dunno what to try anymore!

The problem is: How do I pass a variable into a function which uses the variable to process a MySQL query??

I need to setup a function that I can call up (from about 200 different locations on the page - so it is essential I get it working ;)

I call up the function like this: <?php MakeAverage($ri1); ?>
where ri1 is a column on my MySQL database.

This is the function:

function MakeAverage($warble)
{

$connect = mysql_connect("localhost", "username", "password") or die ("screeewup");

mysql_select_db("dbase");

$query= "SELECT $warble FROM table1";

$results = mysql_query($query) or die (mysql_error());

$counter = 0;

while($rows = mysql_fetch_array($results))
{
extract($rows);
if ($warble < 1)
{
$warble = 1;
$t_ri1 = $t_ri1 + $warble;
$counter+=1;
}
else
{
if (($warble > 5)&&($warble != 9))
{
$warble = 5;
$t_ri1 = $t_ri1 + $warble;
$counter+=1;
}
else
{
$t_ri1 = $t_ri1 + $warble;
$counter+=1;
}
}

}

$raverage1 = round(($t_ri1 / $counter),2);

echo $raverage1."<br>";

}
?>

I started of with no arguments (i.e. $ri1 instead of $warble) and it worked fine, but as soon as I tried feeding ri1 into the function as an argument it stopped working. I.ve tried using &$warble in the function, declaring $warble inside and outside the function, quotemarks in every possible place and way, and loads of other things.... BUT IT WONT WORK!

Help?

Thanks!

  #2 (permalink)  
Old September 27th, 2005, 03:40 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, I figured out the problem on my own after all.... and since I feel a bit foolish, I'll post the explanation:

I was just making things too hard on myself.... I was trying to feed a variable into the function to isolate the values I need from the database query. However, the function doesn't 'understand' that I want the variable to take up the value in the database!

Instead, I can isolate the values in the query itself, so after that I don't need any parameters in the function anymore because the selection from the database contains only the values I need!

I still feel there should be a way to make the first script work.... grumble...


  #3 (permalink)  
Old December 15th, 2005, 03:01 AM
Friend of Wrox
 
Join Date: Mar 2004
Location: Bhopal, MP, India.
Posts: 357
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anshul
Default

You can pass any number of values to a function, one or more arrays also. Some hint: MakeAverage($host, $user, $passwd, $db, $query); or MyQuery($host, $user, $passwd, $db, $query);

`~@#\^%&*/\.<.\/-|+|_!:;..=?>
Students' finances


Similar Threads
Thread Thread Starter Forum Replies Last Post
send variable in function to another function schoolBoy Javascript How-To 6 March 3rd, 2007 09:16 AM
passing variable tbekarov ASP.NET 2.0 Professional 7 February 1st, 2006 04:16 PM
passing variable to function in innerHTML crmpicco Javascript How-To 5 October 19th, 2005 10:25 AM
Passing variable to a public variable Hudson40 Access VBA 2 February 25th, 2005 06:23 AM
Passing a variable into the id() function EstherMStrom XSLT 5 January 19th, 2005 02:32 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.