Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 04-16-2005, 08:30 PM   #1 (permalink)
Buck_NAkie
Registered User
 
Join Date: Apr 2005
Posts: 4
Buck_NAkie is on a distinguished road
Question 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 08:55 PM.
Buck_NAkie is offline   Reply With Quote
Old 04-16-2005, 10:19 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,529
sde is on a distinguished road
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 )
__________________
Mike
sde is offline   Reply With Quote
Old 04-17-2005, 06:35 AM   #3 (permalink)
Buck_NAkie
Registered User
 
Join Date: Apr 2005
Posts: 4
Buck_NAkie is on a distinguished road
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.
Buck_NAkie is offline   Reply With Quote
Old 04-17-2005, 07:20 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,529
sde is on a distinguished road
Quote:
Originally Posted by sde
is it a file just containing 1 number?
does the file look like this:
Code:
50
or is it formatted like this
Code:
10
20
30
40
50
__________________
Mike
sde is offline   Reply With Quote
Old 04-17-2005, 07:39 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-17-2005, 08:00 AM   #6 (permalink)
Buck_NAkie
Registered User
 
Join Date: Apr 2005
Posts: 4
Buck_NAkie is on a distinguished road
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 08:29 AM.
Buck_NAkie is offline   Reply With Quote
Old 04-17-2005, 08:39 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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:
PHP Code:
$count+=$newcount[0]; 
:: 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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-17-2005, 08:56 AM   #8 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
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($fp0);          // rewind to start of file, to prepare for writing
  
fprintf($fp"%d"$count+$newcount);
  
fclose($fp); 

__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 04-17-2005, 08:28 PM   #9 (permalink)
Buck_NAkie
Registered User
 
Join Date: Apr 2005
Posts: 4
Buck_NAkie is on a distinguished road
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
Buck_NAkie is offline   Reply With Quote
Old 04-17-2005, 11:28 PM   #10 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 734
DJMaze is on a distinguished road
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);

DJMaze is offline   Reply With Quote
Reply

Bookmarks

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Craigslist Anonymous Email Relay with PHP & MySQL? oc_earthling PHP 11 03-05-2005 09:10 AM
WML PHP and MySQL rmill9681 PHP 10 11-20-2004 01:59 PM
Simple PHP MySQL code: confused. easilyi Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 4 10-24-2004 08:53 PM
cant connect to mysql databases using php eran PHP 11 08-07-2004 09:02 AM
Help with setting up mySQL and PHP Ilya020 PHP 11 03-19-2003 06:10 AM


All times are GMT -8. The time now is 02:35 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting