Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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!

Reply With Quote
  #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...


Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 12:33 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.