|
 |
|
 |
 |
04-16-2005, 07:30 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 4
|
Please Help I need some help with MySql and PHP
Ok people I am new to programming so if this is really simple please don't laugh.
Hi All below you find a a chunk of code I need help finishing. What I am trying to do is get the total changes in one column called status. I can use the getICMSData to retrieve data then if I could get it to populate the $newcount then output to a .txt file. Right now my code works if I hard code a number into the $newcount. Idealy I would like to get that number from the my GetICMSData or if there is another way that is cool as well. If I am not clear please reply asking questions.
PHP Code:
<?
function CountDefUpdates ()
{
//GetICMSData
UpdateICMS("UPDATE desktops SET status = \"dupend\" WHERE status = \"installed\" AND spy_policy_index = 2",$link);
$newcount =
$file="count.txt";
$fh1=fopen($file,"r");
$count=fscanf($fh1,"%d");
fclose($fh1);
//echo $newcount;
//echo $count[0];
$fh=fopen($file,"w+");
$count[0]=$count[0]+$newcount;
fprintf($fh,"%d",$count[0]);
fclose($fh);
}
?>
__________________
Last edited by Buck_NAkie : 04-16-2005 at 07:55 PM.
|
|
|
04-16-2005, 09:19 PM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,398
|
what does count.txt look like? what exactly is going on in that file? is it a file just containing 1 number? are you sure it is writable by apache or your web server? ( chmod 666 count.txt )
__________________
testing 1 2 3
|
|
|
04-17-2005, 05:35 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 4
|
count.txt is a text file that can be written to. If I hard code a number like this $newcount = (10) then on my text file the count goes up by 10 every time I refresh my screen.
__________________
|
|
|
04-17-2005, 06:20 AM
|
#4 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,398
|
Quote:
|
Originally Posted by sde
is it a file just containing 1 number?
|
does the file look like this: or is it formatted like this
__________________
testing 1 2 3
|
|
|
04-17-2005, 06:39 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,681
|
hmmmm.... What happens for the very first time..
Code:
$newcount =
$file="count.txt";
$fh1=fopen($file,"r");
$count=fscanf($fh1,"%d");
fclose($fh1);
$fh=fopen($file,"w+");
$count[0]=$count[0]+$newcount;
Assuming the count.txt is empty, and since your newcount is empty, theres nothing written to the file, on next read we have the same problem..
If your file contains a number (say 1) then on your $count[0]=$count[0]+$newcount; you have the same problem, the counter isn't incremented, since $newcount isn't holding anything..
On another note, I doubt this code will even run..
$newcount= is an undisclosed statement, which usualy causes an error.
|
|
|
04-17-2005, 07:00 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 4
|
thanks still stuck
Thanks for looking
SDE the out put is a single number that gets overwritten everytime.
redhead:here is the code my question is how do I put a number from my query into newcount
PHP Code:
function CountDefUpdates ()
{
$newcount = GetICMSData ("SELECT COUNT(*) FROM desktops WHERE status=\"installed\"",$link);
UpdateICMS("UPDATE desktops SET status = \"dupend\" WHERE status = \"installed\"AND spy_policy_index = 2",$link);
//$newcount =(10);
$file="count.txt";
$fh1=fopen($file,"r");
$count=fscanf($fh1,"%d");
fclose($fh1);
//echo $newcount;
//echo $count[0];
$fh=fopen($file,"w+");
$count[0]=$count[0]+$newcount;
fprintf($fh,"%d",$count[0]);
fclose($fh);
}
__________________
Last edited by redhead : 04-17-2005 at 07:29 AM.
|
|
|
04-17-2005, 07:39 AM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,681
|
hmmm.. I havn't used this type of account holding, but from my point the $count=fscanf($fh1,"%d"); will let $count hold the value in the file (if the file only holds one number) thus the statement $count[0] won't return the correct value, instead $count will be the way to get the number.
How about something like:
PHP Code:
fscanf($fh1, "%[0-9]", $count);
Your $newcount should be formed in the way, where $newcount[0] is holding the actual value, thus you would need to make your increment line something like:
:: edit
I just realize, I dont even know what your GetICMSData() is doing, so I can't comment on wether $newcount is the value or $newcount[0], my suggestion was formed given the fact, that
PHP Code:
$res = mysql_query("SELECT COUNT(*)...");
value = mysql_fetch_array($res);
makes $value hold the number in the way I describe $newcount.
|
|
|
04-17-2005, 07:56 AM
|
#8 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,681
|
I just looked through the code, and if I were to make it I'd probably do it something like this, assuming GetICMSData() is returning the number instead of an array with the number.
PHP Code:
function CountDefUpdates ()
{
$newcount = GetICMSData ("SELECT COUNT(*) FROM desktops WHERE status=\"installed\"",$link);
UpdateICMS("UPDATE desktops SET status = \"dupend\" WHERE status = \"installed\"AND spy_policy_index = 2",$link);
$file="count.txt";
$fp=fopen($file,"r+"); // make it read and write accessible
$ret = fscanf($fp, "%[0-9]", $count);
if(!$ret)
$count = 0; // catch if the file is empty
fseek($fp, 0); // rewind to start of file, to prepare for writing
fprintf($fp, "%d", $count+$newcount);
fclose($fp);
}
|
|
|
04-17-2005, 07:28 PM
|
#9 (permalink)
|
|
Registered User
Join Date: Apr 2005
Posts: 4
|
thanks for the help but i quess this is way over my head. I cannot get the output to show up in the count.txt.
thanks again
__________________
|
|
|
04-17-2005, 10:28 PM
|
#10 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 637
|
it's easy
PHP Code:
$res = mysql_query("SELECT COUNT(*)...");
// mysql_fetch_array and mysql_fetch_assoc are slower and not important here
list($newcount) = mysql_fetch_row($res);
++$newcount; // increase the number
$file="count.txt";
if ($fh=fopen($file,"w+")) {
fwrite($fh,$newcount);
fclose($fh);
}
__________________
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 11:54 AM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|