Passing Variables to Querys

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
davephp
Forum Newbie
Posts: 3
Joined: Thu Aug 31, 2006 3:42 am

Passing Variables to Querys

Post by davephp »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I have created a query below:   I need to know how to pass different values of $monthid to the query.

Code: Select all

<?php
$currentuser_GetData = "dave";
if (isset($_SESSION['MM_Username'])) {
  $currentuser_GetData = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
$monthnum_GetData = "6";
if (isset($monthid)) {
  $monthnum_GetData = (get_magic_quotes_gpc()) ? $monthid : addslashes($monthid);
}
mysql_select_db($database_Connect, $Connect);
$query_GetData = sprintf("SELECT sum(transactions.amount) FROM transactions WHERE MONTH(transactions.date) = %s AND transactions.userlogged = \"%s\"", $monthnum_GetData,$currentuser_GetData);
$GetData = mysql_query($query_GetData, $Connect) or die(mysql_error());
$row_GetData = mysql_fetch_assoc($GetData);
$totalRows_GetData = mysql_num_rows($GetData);
Basically I want the query to run 12 times on the same page, and display the resuts in 12 different places. I could repeat this code 12 times but is there a way to use an array or something similar?

The data is in a table of transactions throughout the year and I want to retrieve totals per month per user. (and eventually per category as well, but I haven't got to that yet.)

Hope this makes sense!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

You already have the data in an array, $row_GetData. What exactly are you trying to do with this?
davephp
Forum Newbie
Posts: 3
Joined: Thu Aug 31, 2006 3:42 am

Post by davephp »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I want to create a table that displays totals of all the transactions in June for a specific user.

Code: Select all

Jan =        <?php echo $row_GetData['sum(transactions.amount)']; ?> (With 1 passed to $monthid)
Feb =        <?php echo $row_GetData['sum(transactions.amount)']; ?> (With 2 passed to $monthid)
Mar =        <?php echo $row_GetData['sum(transactions.amount)']; ?> (With 3 passed to $monthid)
Does that make any more sense?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
davephp
Forum Newbie
Posts: 3
Joined: Thu Aug 31, 2006 3:42 am

Post by davephp »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I changed my query and it works almost how I wanted it to.  It will do.

Thanks Blackbeard for the help.

Code: Select all

<?php
$currentuser_GetData = "dave";
if (isset($_SESSION['MM_Username'])) {
  $currentuser_GetData = (get_magic_quotes_gpc()) ? $_SESSION['MM_Username'] : addslashes($_SESSION['MM_Username']);
}
mysql_select_db($database_Connect, $Connect);
$query_GetData = sprintf("SELECT sum(transactions.amount), MONTH(date) FROM transactions WHERE transactions.userlogged = \"%s\" GROUP BY MONTH(transactions.date)", $currentuser_GetData);
$GetData = mysql_query($query_GetData, $Connect) or die(mysql_error());
$row_GetData = mysql_fetch_assoc($GetData);
$totalRows_GetData = mysql_num_rows($GetData);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply