Page 1 of 1

Code Using a Database

Posted: Wed Feb 23, 2005 5:48 pm
by Nukes
Hey guys, this code will help you manage your guild data in a MySQL database. I do need some help in reading the "guildinfo.csv" file and putting the information into a table on the database. We also need to write this so it updates current members and adds new members as it reads. Here's what I have so far.

Code: Select all

<?php

include&#40;'db_connect.php'&#41;;

global $guild_id;
global $local_timestamp;
global $remote_timestamp;

//////////////////////////////////////////////////////////////////
// SQL TABLE SETUP                                              //
// Census = id, date                                            //
// guild_roster = id, name, race, class, rank, level, last_seen //
//////////////////////////////////////////////////////////////////

// Resets the local census status field to null - Debuging Only
function ResetLocal&#40;&#41;
&#123;  $q = 'UPDATE `Census` SET `date` = "" WHERE `id` = 1 LIMIT 1';
   $result = mysql_query&#40;$q&#41;;
   if &#40;!$result&#41;
   &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
      exit;
   &#125;
&#125;

// Updates the local status field to variable $date
function UpdateLocal&#40;$date&#41;
&#123;  $q = 'UPDATE `Census` SET `date` = "'.$date.'" WHERE `id` = 1 LIMIT 1';
   $result = mysql_query&#40;$q&#41;;
   if &#40;!$result&#41;
   &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
      exit;
   &#125;
   echo'<br>Local TimeStamp Updated';
&#125;

// Reads remote guildinfo.csv file and exports information to database
function GetRemoteData&#40;&#41;
&#123;  echo'<br>Getting Data From Census Server';
   //  NEED HELP WITH THIS CODE
   //  NEED HELP WITH THIS CODE
&#125;

// Creates a table with guild information from local database
function GetLocalData&#40;&#41;
&#123;  echo'<br>Getting Data From Local Database';
   $q = 'SELECT `name`,`race`,`class`,`rank`,`level`,`last_seen` FROM `guild_roster` ORDER BY `level` ASC ';
   $result = mysql_query&#40;$q&#41;;
   if &#40;!$result&#41;
   &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
      exit;
   &#125;
   echo'<hr>';
   echo'<center>';
   echo'<h1>Invictus Mortem</h1>';
   echo'<table border=1>';
   echo'<tr><td>Name</td><td>Race</td><td>Class</td><td>Rank</td><td>Level</td><td>Last Seen</td></tr>';
   echo'<tr>';
   $rows = mysql_num_fields&#40;$result&#41;;
   while &#40;$info = mysql_fetch_row&#40;$result&#41;&#41;
   &#123;  for&#40;$i=0; $i<$rows-1;$i++&#41;
      &#123;  echo'<td>'.$info&#91;$i&#93;.'</td>';
      &#125;
      echo'<td>'.strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$info&#91;$rows&#93;&#41;.'</td>';
      echo'</tr>';
   &#125;
   echo'</table>';
   echo'</center>';
&#125;

// Reads Local Date
function GetLocalDate&#40;&#41;
&#123;  global $local_timestamp;
   $q = "SELECT * FROM Census WHERE id=1";
   $result = mysql_query&#40;$q&#41;;
   if &#40;!$result&#41;
   &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
      exit;
   &#125;
   
   $info = mysql_fetch_row&#40;$result&#41;;
   $local_timestamp = $info&#91;1&#93;;

   if&#40;$info&#91;1&#93;==null&#41;
   &#123;  echo'First Time Running';
      $q = 'UPDATE `Census` SET `date` = "0000000000" WHERE `id` = 1 LIMIT 1';
      $result = mysql_query&#40;$q&#41;;
      if &#40;!$result&#41;
      &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
         exit;
      &#125;
      $local_timestamp = "00000000";
      echo '<br>Rest Timestamp';
      echo '<br>Local Time Stamp &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$local_timestamp&#41; . '<br>';
   &#125;
   else
   &#123;  echo 'TimeStamp&#58; '.$info&#91;1&#93;;
      echo '<br>Local Time Stamp &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$local_timestamp&#41; . '<br>';
      
   &#125;
&#125;

// Reads Remote Date
function GetRemoteDate&#40;&#41;
&#123;  global $remote_timestamp;
   $filename = "http&#58;//www.warcraftrealms.com/exports/status.txt";
   $infile = fopen &#40;$filename, "r"&#41;;
   if &#40;!$infile&#41;
   &#123;  echo "<p>REMOTE&#58; Unable to open status file.<br>";
      exit;
    &#125;
    if&#40;!feof &#40;$infile&#41;&#41;
    &#123;   $buffer = fgets&#40;$infile, 4096&#41;;
        $remote_timestamp = trim&#40; $buffer &#41;;
        echo 'Remote Time Stamp &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$remote_timestamp&#41; . '<br>';
    &#125;
    fclose&#40; $infile &#41;;
&#125;

// Checks Time Stamps and Determines what to do
function CheckTimeStamps&#40;&#41;
&#123;  global $local_timestamp;
   global $remote_timestamp;
   echo'<br>';
   echo'Checking Time Stamps....';
   echo'<br>Local&#58; '.$local_timestamp;
   echo'<br>Reomote&#58; '.$remote_timestamp;
   $time = $remote_timestamp - $local_timestamp;
   echo'<br>Difference in time strings&#58; '.$time;
   if&#40; $time > 43200&#41;
   &#123;  echo'<br>Update Needed';
      UpdateLocal&#40;$remote_timestamp&#41;;
      GetRemoteData&#40;&#41;;
      GetLocalData&#40;&#41;;
   &#125;
   else
   &#123;  echo'<br>Update Not Needed';
      GetLocalData&#40;&#41;;
   &#125;
&#125;

//ResetLocal&#40;&#41;;   // Uncomment for Debugging Only
GetLocalDate&#40;&#41;;
GetRemoteDate&#40;&#41;;
CheckTimeStamps&#40;&#41;;

?>

Posted: Wed Feb 23, 2005 8:09 pm
by Rollie
I didn't test this code, but this should do what you need it to, even if there are some unseen syntax errors =)

Code: Select all

    $filename = 'http&#58;//www.warcraftrealms.com/exports/guildexport.php?guildid=' . $guild_id; 
    
    $infile = fopen &#40;$filename, "r"&#41;; 
    if &#40;!$infile&#41; 
    &#123; 
        echo "<p>Unable to open remote file.<br>\n"; 
        exit; 
    &#125; 

    //  do one read to get the header 
    $buffer = fgets&#40;$infile, 4096&#41;; 

    //  read the entries 
    while &#40;!feof &#40;$infile&#41;&#41; 
    &#123; 
        $buffer = fgets&#40;$infile, 4096&#41;; 
        list&#40; $name, $race, $class, $level, $last_seen, $rank &#41; 
                        = explode&#40;",",$buffer&#41;; 

        $query = "SELECT id FROM guild_roster WHERE name = $name";
        $result = mysql_query&#40;$query&#41; 
                       or die &#40;"Error unable to select " . mysql_error&#40;&#41; . " <br />\n"&#41;;
        if &#40;!$result&#41; 
        &#123;
            die&#40;'Could not query&#58;' . mysql_error&#40;&#41;&#41;;
        &#125;

        if&#40; ! $row = mysql_fetch_row&#40;$result&#41;&#41;
        &#123;
            //  there is no entry for this character, so insert
            $query = "INSERT INTO guild_roster &#40; name, race, class, rank, level, last_seen&#41; " .
                     "VALUES &#40;$name, $race, $class, $rank, $level, $last_seen&#41;";
            mysql_query&#40;$query&#41; 
                      or die &#40;"Error unable to insert " . mysql_error&#40;&#41; . " <br />\n"&#41;;
            
            echo "Inserting new entry => $name<br />\n";
        &#125;
        else
        &#123;
            //  this character is already present, let's just update him
            $charid = $row&#91;0&#93;;
            $query = "UPDATE guild_roster SET rank = $rank, level = $level, last_seen = $last_seen WHERE id = $charid";

            mysql_query&#40;$query&#41; 
                        or die &#40;"Error unable to update " . mysql_error&#40;&#41; . " <br />\n"&#41;;

            echo "Updating existing entry => $name<br />\n";
        &#125;
    &#125; 
    fclose&#40;$infile&#41;; 
Depending on how you have the indexes set up on the table, you could do it all with a single REPLACE statement.

Posted: Fri Mar 04, 2005 11:24 pm
by Nukes
Getting Data From Census ServerError unable to select You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Posted: Fri Mar 04, 2005 11:52 pm
by Guest
I'm having trouble getting the query statements right. I am using MySQL version 2.6.1-rc2. The problem is all the single quotes and appending PHP variables. Any help would be appreciated.

Posted: Sat Mar 05, 2005 12:10 am
by Nukes
Here's my code, however, when it updates there are some issues with update speed. As it goes through the document it sometimes stops halfway through. Therefore not everything is updated. In order to get all the information on the database I would need to uncomment the resetlocaltime line which would then cause the update everytime (which you do not want us to do). Let me know if you have any solutions.

Code: Select all

<?php

include&#40;'db_connect.php'&#41;;
// 64361
global $guild_id;
global $local_timestamp;
global $remote_timestamp;

//////////////////////////////////////////////////////////////////
// SQL TABLE SETUP                                              //
// Census = id, date                                            //
// guild_roster = id, name, race, class, rank, level, last_seen //
//////////////////////////////////////////////////////////////////

// Resets the local census status field to null - Debuging Only
function ResetLocal&#40;&#41;
&#123;  $q = 'UPDATE `Census` SET `date` = "" WHERE `id` = 1 LIMIT 1';
   $result = mysql_query&#40;$q&#41;;
   if &#40;!$result&#41;
   &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
      exit;
   &#125;
&#125;

// Updates the local status field to variable $date
function UpdateLocal&#40;$date&#41;
&#123;  $q = 'UPDATE `Census` SET `date` = "'.$date.'" WHERE `id` = 1 LIMIT 1';
   $result = mysql_query&#40;$q&#41;;
   if &#40;!$result&#41;
   &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
      exit;
   &#125;
   echo'<br>Local TimeStamp Updated';
&#125;

// Reads remote guildinfo.csv file and exports information to database
function GetRemoteData&#40;&#41;
&#123;  $guild_id=64361;
   echo'<br>Getting Data From Census Server';
   $filename = 'http&#58;//www.warcraftrealms.com/exports/guildexport.php?guildid='.$guild_id;
   $infile = fopen &#40;$filename, "r"&#41;;
   if &#40;!$infile&#41;
   &#123;  echo "<p>Unable to open remote file.<br>\n";
      exit;
   &#125;
   //  do one read to get the header
   $buffer = fgets&#40;$infile, 4096&#41;;
   //  read the entries
   while &#40;!feof &#40;$infile&#41;&#41;
   &#123;  $buffer = fgets&#40;$infile, 4096&#41;;
      list&#40; $name, $race, $class, $level, $last_seen, $rank &#41; = explode&#40;",",$buffer&#41;;
      echo'<br>NAME&#58; '.$name;
      echo'<br>RACE&#58; '.$race;
      echo'<br>CLASS&#58; '.$class;
      echo'<br>LEVEL&#58; '.$level;
      echo'<br>LAST SEEN&#58; '.$last_seen;
      echo'<br>RANK&#58; '.$rank;
      $query = "SELECT id FROM `guild_roster` WHERE name = '$name'";
      //echo'<br>QUERY&#58; '.$query;
      $result = mysql_query&#40;$query&#41; or die &#40;"<br>Error unable to select " . mysql_error&#40;&#41; . " <br />\n"&#41;;
      if &#40;!$result&#41;
      &#123;  die&#40;'Could not query&#58;' . mysql_error&#40;&#41;&#41;;
      &#125;
      if&#40; ! $row = mysql_fetch_row&#40;$result&#41;&#41;
      &#123;  //  there is no entry for this character, so insert
         $query = "INSERT INTO guild_roster &#40; name, race, class, rank, level, last_seen&#41; " .
                     "VALUES &#40;'$name', '$race', '$class', '$rank', '$level', '$last_seen'&#41;";
         echo'<br>QUERY&#58; '.$query;
	 mysql_query&#40;$query&#41; or die &#40;"<br>Error unable to insert " . mysql_error&#40;&#41; . " <br />\n"&#41;;
         echo "Inserting new entry => $name<br />\n";
      &#125;
      else
      &#123;  //  this character is already present, let's just update him
         $charid = $row&#91;0&#93;;
         echo'<br>CHAR ID&#58; '.$charid;
         $query = "UPDATE guild_roster SET rank = '$rank', level = '$level', last_seen = '$last_seen' WHERE id = '$charid'";
         mysql_query&#40;$query&#41; or die &#40;"<br>Error unable to update " . mysql_error&#40;&#41; . " <br />\n"&#41;;
         echo "Updating existing entry => $name<br />\n";
      &#125;
    &#125;
    fclose&#40;$infile&#41;;
&#125;

// Creates a table with guild information from local database
function GetLocalData&#40;&#41;
&#123;  echo'<br>Getting Data From Local Database';
   $q = 'SELECT `name`,`race`,`class`,`rank`,`level`,`last_seen` FROM `guild_roster` ORDER BY `class` ';
   $result = mysql_query&#40;$q&#41;;
   if &#40;!$result&#41;
   &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
      exit;
   &#125;
   echo'<hr>';
   echo'<center>';
   echo'<h1>Invictus Mortem</h1>';
   echo'<table border=1>';
   echo'<tr><td>Name</td><td>Race</td><td>Class</td><td>Rank</td><td>Level</td><td>Last Seen</td></tr>';
   echo'<tr>';
   $rows = mysql_num_fields&#40;$result&#41;;
   while &#40;$info = mysql_fetch_row&#40;$result&#41;&#41;
   &#123;  for&#40;$i=0; $i<$rows;$i++&#41;
      &#123;  echo'<td>'.$info&#91;$i&#93;.'</td>';
      &#125;
      //echo'<td>'.strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$info&#91;$rows&#93;&#41;.'</td>';
      echo'</tr>';
   &#125;
   echo'</table>';
   echo'</center>';
&#125;

// Reads Local Date
function GetLocalDate&#40;&#41;
&#123;  global $local_timestamp;
   $q = "SELECT * FROM Census WHERE id=1";
   $result = mysql_query&#40;$q&#41;;
   if &#40;!$result&#41;
   &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
      exit;
   &#125;
   
   $info = mysql_fetch_row&#40;$result&#41;;
   $local_timestamp = $info&#91;1&#93;;

   if&#40;$info&#91;1&#93;==null&#41;
   &#123;  echo'First Time Running';
      $q = 'UPDATE `Census` SET `date` = "0000000000" WHERE `id` = 1 LIMIT 1';
      $result = mysql_query&#40;$q&#41;;
      if &#40;!$result&#41;
      &#123;  echo 'ERROR&#58; Could not run query&#58; '.mysql_error&#40;&#41;;
         exit;
      &#125;
      $local_timestamp = "00000000";
      echo '<br>Rest Timestamp';
      echo '<br>Local Time Stamp &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$local_timestamp&#41; . '<br>';
   &#125;
   else
   &#123;  echo 'TimeStamp&#58; '.$info&#91;1&#93;;
      echo '<br>Local Time Stamp &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$local_timestamp&#41; . '<br>';
      
   &#125;
&#125;

// Reads Remote Date
function GetRemoteDate&#40;&#41;
&#123;  global $remote_timestamp;
   $filename = "http&#58;//www.warcraftrealms.com/exports/status.txt";
   $infile = fopen &#40;$filename, "r"&#41;;
   if &#40;!$infile&#41;
   &#123;  echo "<p>REMOTE&#58; Unable to open status file.<br>";
      exit;
    &#125;
    if&#40;!feof &#40;$infile&#41;&#41;
    &#123;   $buffer = fgets&#40;$infile, 4096&#41;;
        $remote_timestamp = trim&#40; $buffer &#41;;
        echo 'Remote Time Stamp &#58; ' . strftime&#40;"%m/%d/%y %H&#58;%M&#58;%S",$remote_timestamp&#41; . '<br>';
    &#125;
    fclose&#40; $infile &#41;;
&#125;

// Checks Time Stamps and Determines what to do
function CheckTimeStamps&#40;&#41;
&#123;  global $local_timestamp;
   global $remote_timestamp;
   echo'<br>';
   echo'Checking Time Stamps....';
   echo'<br>Local&#58; '.$local_timestamp;
   echo'<br>Reomote&#58; '.$remote_timestamp;
   $time = $remote_timestamp - $local_timestamp;
   echo'<br>Difference in time strings&#58; '.$time;
   if&#40; $time > 43200&#41;
   &#123;  echo'<br>Update Needed';
      UpdateLocal&#40;$remote_timestamp&#41;;
      GetRemoteData&#40;&#41;;
      GetLocalData&#40;&#41;;
   &#125;
   else
   &#123;  echo'<br>Update Not Needed';
      GetLocalData&#40;&#41;;
   &#125;
&#125;

//ResetLocal&#40;&#41;;   // Uncomment for Debugging Only
GetLocalDate&#40;&#41;;
GetRemoteDate&#40;&#41;;
CheckTimeStamps&#40;&#41;;

?>

Posted: Sat Mar 05, 2005 12:22 am
by Guest
Nevermind the issues I had, was with a setting in my PHP.ini. That last code that I posted works great. Just comment out some of the unnecessary echo lines. Thanks for the help on that Rollie.

Posted: Mon Mar 21, 2005 1:13 pm
by Rollie
One easy way to do that would be to delete all entries each time before you update it.

Posted: Sat Apr 30, 2005 5:09 am
by Vimes
Hi There!

I did a solution that clears the table completely, so removing chars no longer in the guild according to census data.

the main file "roster.php" will include the "getdata.php" whenever the file is called and the timestamp in the database is not corresponding with the current timestamp of the census data, therefore not loading/processing the code for updating when not needed

Code: Select all

$filename = "http&#58;//www.warcraftrealms.com/exports/status.txt";
$fp = fopen&#40;$filename,"r"&#41;;
$status = fread&#40;$fp,65535&#41;;
$query = "SELECT * FROM timestamp WHERE timestamp = $status";
$result = mysql_query&#40;$query&#41;;
$count=mysql_numrows&#40;$result&#41;;
if &#40;$count == 0&#41;
&#123;
include &#40;"getdata.php"&#41;;
&#125;
the getdata.php looks like this:

Code: Select all

<?php$row = 1;
$i = 0;    // used to clear headers from csv file
$handle = fopen &#40;"http&#58;//www.warcraftrealms.com/exports/guildexport.php?guildid=foobar","r"&#41;;              

// clear table first
$clear = "TRUNCATE TABLE characters";
mysql_query&#40;$clear&#41;;

// insert new data
while &#40; &#40;$data = fgetcsv &#40;$handle, 1000, ","&#41;&#41; !== FALSE &#41; &#123; 
	if &#40;$i != 0&#41; // $i will be 0 when column headers are provided 
	&#123;
		$update="INSERT INTO characters &#40;CharName, Race, Class, Level, LastSeen, GuildRank&#41; VALUES &#40;'$data&#91;0&#93;','$data&#91;1&#93;','$data&#91;2&#93;','$data&#91;3&#93;','$data&#91;4&#93;','$data&#91;5&#93;'&#41;";
		mysql_query &#40;$update&#41;;
	&#125;
	$i++;
&#125;
fclose &#40;$handle&#41;;


// update the timestamp in the database
$updatestatus="UPDATE timestamp SET timestamp = $status";
mysql_query &#40;$updatestatus&#41;;

?>
the performance would be better using inserts and updates of course, but if your guild does not have 5K+ members you will not feel it i guess ;)

you can find a running copy here:
http://www.warmuth.cc/roster/roster.php
The running version includes a column representing the char as a "main" or "alt of <maincharname>"

hope this helps, cheers, Vimes

P.S.: I did the obligatory link in german language on the page, i hope you don't mind ;)

Posted: Sat Apr 30, 2005 12:59 pm
by Rollie
Long as the link is there, I'm happy =) Any language will do! Cept maybe Swahili.... wonder what that would look like anyway... hrm...

Posted: Thu Sep 01, 2005 8:58 am
by Precisi
I have SOME sql experience, but I'm a bit confused on what to do with all this code. Is anyone willing to write up some simple instructions like..

Step 1: Make a database with these fields..
Step 2: Use this code named as this file..
etc.

It would be greatly appreciated. I would like to get a nice guild listing on my guilds site (redesigning the whole thing or I would link to it) and then once I get everything all set up I'm willing to provide all the files in a tidy zip with easy instructions for other people like me who "don't quite get it".

I have a basic guild roster running here: http://closetgamers.snowshoedesign.com/census/

I used Schwarzhuf's code, but I would like to offer sorting and whatnot with a database.

Any guidance is appreciated!

Posted: Sun Nov 27, 2005 4:57 pm
by Praxi
Just a quick noobie question here. How do I know what database the characters table is going into? It would be nice if the database scripts and instructions for this part would still be updated, and maybe some more examples! :)

My Background:
I know HTML decently, I can follow php code (or know where to look to figure out what its doing), mysql I'm so so with (mainly do anything required through myphpadmin.

Posted: Thu Jan 12, 2006 8:31 am
by Groyff
Hi!
Perhaps I should post this at a sql/php forum. But I'll try here first.

Trying to create a workaround to the remote file access problem.
Since our guild forums host don't allow fopen and don't have cURL installed can I for the Guild Roster point to a sql-database located on a different server?
Or even better, could I have a php-script situated elsewhere uploading roster info to a the database at the forum host.


Cheers,
Groyff

Posted: Thu Jan 12, 2006 11:18 am
by Ceto
If you have shell access, you could probably write a bash/python/etc. script to download the roster. You can even have the script spawn whatever PHP script parses the file, set it all up via cron for automatic updates ever 12 hours or so.

Or, here's a crazy idea: create a web page with a textarea form field. Set a JavaScript onload event that checks for an updated roster, fetches that roster via HTTP, and inserts the content into the form field. On submit, the data goes to a PHP script that injects the roster into your database. That way you're sidestepping all the server-side issues and providing the data via your web browser, sort of like uploading the census .txt file but with less intervention.

Posted: Fri Jan 13, 2006 6:27 am
by Groyff
Thanks for the tip Ceto. For now I've done it so I have the rosterupdater at a server with remote file access, but the form thing sounds like good idea, gonna check in to it when I got then time.

The roster can be seen at http://www.violence-reborn.co.uk
oh, and I'm gonna add info that data is fetched from www.warcraftrealms.com aswell, just slipped my mind.

Can I get the php files?

Posted: Thu Jun 01, 2006 3:13 pm
by dantoonina
Groyff,
Can you do me a favor? Can I get a copy of the php files? If you zip them up, I would like to use yours on our guild site. I don't know much, but if you can add a not how to install it to a database I can create, I think I can get it running. Can you send it to me at my email address? Thanks in advance.

Anthony
Groyff wrote:Thanks for the tip Ceto. For now I've done it so I have the rosterupdater at a server with remote file access, but the form thing sounds like good idea, gonna check in to it when I got then time.

The roster can be seen at http://www.violence-reborn.co.uk
oh, and I'm gonna add info that data is fetched from www.warcraftrealms.com aswell, just slipped my mind.

Posted: Mon Oct 23, 2006 5:15 am
by Groyff
I guess this request is obsolete, dantoonina?
If you still want them I'll send'em

/G