mysql query in a function

This is a discussion on "mysql query in a function" within the PHP Forum section. This forum, and the thread "mysql query in a function are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old May 17th, 2006, 11:02
New Member
Join Date: May 2006
Location: edinburgh
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mysql query in a function

having problems with the following function. When called once it works no problem, but on multiple calls, it fails on the second run of the query. The file outputs the first run then nothing. Any help is appreciated.
PHP: Select all

function getStats($start,$no,$file,$group,$reg,$type,$freq){
require_once(
'Connections/carhire.php');
for (
$i=0$i<$no$i++){
$temps explode("/",$start); //split end date
if ($freq == 'daily'){$end $start;} //same day end
if ($freq == 'weekly'){
$end date("Y\/m\/d",mktime(0,0,0,$temps[1],$temps[2]+6,$temps[0]));} //end date is 6 days from start
if ($freq == 'monthly'){
$end date("Y\/m\/d",mktime(0,0,0,$temps[1]+1,0,$temps[0]));} //end date is last day of month
if ($freq == 'yearly'){
$end date("Y\/m\/d",mktime(0,0,0,12,31,$temps[0]));} //end date is last day of year
if($type == 'income'){
mysql_select_db($database_carhire$carhire);
$query_incomestats "SELECT COUNT(total) AS no, SUM(total) as total FROM Rental WHERE sdate >= '$start' AND sdate <= '$end'";
$incomestats mysql_query($query_incomestats$carhire) or die(mysql_error());
$row_incomestats mysql_fetch_assoc($incomestats);
$totalRows_incomestats mysql_num_rows($incomestats);
}
 
if(
$type == 'vehicle'){
mysql_select_db($database_carhire$carhire);
$query_vehiclestats "SELECT COUNT(total) AS no, SUM(total) AS total FROM Rental WHERE reg = '$reg' AND sdate >= '$start' AND sdate <= '$end'";
$vehiclestats mysql_query($query_vehiclestats$carhire) or die(mysql_error());
$row_vehiclestats mysql_fetch_assoc($vehiclestats);
$totalRows_vehiclestats mysql_num_rows($vehiclestats);
}
if (
$type == 'group'){
mysql_select_db($database_carhire$carhire);
$query_groupstats "SELECT COUNT(total) AS no, SUM(total) AS total FROM Rental R WHERE no = '$group' AND sdate >= '$start' AND sdate <= '$end'";
$groupstats mysql_query($query_groupstats$carhire) or die(mysql_error()); //error occurs here
$row_groupstats mysql_fetch_assoc($groupstats);
$totalRows_groupstats mysql_num_rows($groupstats);
}
if(
$type == 'income'){
fwrite($file,$start.",".$row_incomestats['no'].",".$row_incomestats['total']."\n");}
if(
$type == 'group'){
fwrite($file,$group.",".$start.",".$row_groupstats['no'].",".$row_groupstats['total']."\n");}
if(
$type == 'vehicle'){
fwrite($file,$reg.",".$start.",".$row_groupstats['no'].",".$row_groupstats['total']."\n");}
 
$tempe explode("/",$end);
$start date("Y\/m\/d",mktime(0,0,0,$tempe[1],$tempe[2]+1,$tempe[0]));
 
mysql_free_result($incomestats);
mysql_free_result($vehiclestats);
mysql_free_result($groupstats);
}
}
 
if (
$type == "group" || $type == 'all'){
$filename "Group_".$fstart."_".$fend.".csv";
$file fopen("Stats/".$filename,w);
for (
$i=1;$i<5;$i++){
fwrite($file,"\"Group No\",\"Start Date\",\"No of Bookings\",\"Income\"\n");
getStats($start,$no,$file,$i,0,"group",$freq);
fwrite($file,"\n");
}
fclose($file);
$message $message."Group Statistics Output to<br>".$filename."<br>"

Last edited by sypher; May 18th, 2006 at 04:16.
Reply With Quote

  #2 (permalink)  
Old May 18th, 2006, 10:33
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: mysql query in a function

Unless I'm missing something, each time you call the function, the queries are run a new so you will get the first row in the returned set of rows every time.

The same is true of your for...loop.
Reply With Quote
  #3 (permalink)  
Old May 18th, 2006, 12:44
New Member
Join Date: May 2006
Location: edinburgh
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: mysql query in a function

You are missing something. The first row is all i need as it contains the values returned by the two functions. on each loop, the variables passed to the query are different and so different results are generated. the problem isn't the internal loop of the function. it seems to be if the function is called more than once then the query causes an error.
Reply With Quote
  #4 (permalink)  
Old May 18th, 2006, 14:43
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: mysql query in a function

Might be helpful if you told us what the error was!
Reply With Quote
  #5 (permalink)  
Old May 18th, 2006, 15:27
New Member
Join Date: May 2006
Location: edinburgh
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: mysql query in a function

a blank page
Reply With Quote
  #6 (permalink)  
Old May 18th, 2006, 15:43
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Skype™ to ukgeoff
Re: mysql query in a function

I was thinking more in terms of a mysql error as to why it's not returning any data, if in fact that is the case.
Reply With Quote
  #7 (permalink)  
Old May 19th, 2006, 01:25
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: mysql query in a function

I suspect that the problem is with the require_once and the code you haven't shown us. This code only runs once - the first time you call the getStats function. Does it, by any chance, open the database connection and and set variables such as $carhire? If it does, then they're only set the first time you run the function, lost when the function exits, then not created again the second time. Solution - switch to require.

-- Graham
Reply With Quote
Reply

Tags
mysql, query, function

Thread Tools

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Mysql Query not working nate2099 Databases 13 Jan 3rd, 2008 02:28
MYSQL/PHP Query question andrewlondon Databases 1 Sep 19th, 2007 11:26
MYSQL PHP query class jhappeal Databases 1 Aug 17th, 2007 01:52
MySQL query query dangergeek Databases 3 Apr 12th, 2007 12:45
Returning a value from a MYSQL Query? Kimochi PHP Forum 7 Feb 28th, 2007 20:09


All times are GMT. The time now is 07:27.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43