Introduction: This is a detailed page of counters Based on files and databases. It introduces PHP, related knowledge, skills, experience, and some PHP source code.
Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 323740 'rolling = 'no'>
Original at www.dukejava.com
File-based PHP counters
<〈?
// PHP counters, based on the file system.
Function wincounter (){
// If the object does not exist, create it
If (! File_exists ("count.txt "))
{
Exec ("Echo 0> count.txt ");
}
// Open our record file
// Obtain the file size and obtain the required data based on the file size.
$ Fp = fopen ("count.txt", "R + );
$ Filesize = filesize ("count.txt ");
$ COUNT = fgets ($ FP, $ filesize + 1 );
// Add the number of records to 1 and save the records to the file.
$ Count + = 1;
Fseek ($ FP, $ count );
Fclose ($ FP );
// Returns the number of shards.
Return $ count;
}
? > 〉
Database-based counters (MySQL )?
1. Create a database first:
Create Table counter {
Counter int not null,
Id int not null
}
Insert into counter (counter, ID) values (0, 1)
2. countersCode:
<〈?
// PHP counter, based on the MySQL database server.
Function linuxcounter (){
// Connect to the MySQL database
$ Conn = mysql_connect ("localhost", phpbook ","");
// Query the current number of views
// Note the method for obtaining the result
$ SQL = "select * from counter ";
$ Result = mysql_query ($ SQL, $ conn );
$ Objresult = mysql_fetch_object ($ result );
$ COUNT = $ objresult-> counter;
// Update the database and return the current browser count as the result
$ SQL = "update counter set counter =". ($ cont + 1). "Where id = 1 ";
Mysql_query ($ SQL, $ conn );
Mysql_close ($ conn );
Return $ count + 1;
}
More articles on "file-and database-based counters"
Love J2EE follow Java Michael Jackson video station JSON online tools
Http://biancheng.dnbcw.info/php/323740.html pageno: 15.