Jump to content

[SOLVED] Logging Data into a text file from a Database


Monk3h

Recommended Posts

I am currently using the Delete from Command to clear chat on my game. Is there a way for me to Log all the data into a text file and then delete it all from the Chat Table in my Database thru PHP?

 

I mean asin a tool i can make to do it for me from my Admin Options in game so other Admins can do the same?

Link to comment
Share on other sites

this is an example:

<?php
$chat_user = array();
$chat_data = array();
$sql = "SELECT * FROM chat;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
$chat_user[] .= $row['user'];
$chat_log[] .= $row['log']
}
$count = 0;
$file_data = "";
foreach ($chat_user as $value){
$file_data .= $value.":".$chat_log[$count]."\n";
$count++;
}
$file = "log_".date("Y-m-D").".txt";
$directory ="path/to/saves/"; //must be writeable
$handle = fopen ($directory.$file, "w+");
fwrite ($handle, $file_data);
fclose($handle);
?>

Link to comment
Share on other sites

I dont understand that.. :/

 

This is my current Script to Clear chat. Could you add it to to that so i can see what does what the the Values im using? Or explain what each party does? Im not just trying to get you to do my coding for me i Just want to understand what does what. :)

 

if ($view == clearc) {
mysql_query("delete from chat");
mysql_query("insert into chat (id, user, chat) values('1', '<span style=color:#FF0000>Admin</span>', '<span style=color:#FF0000><b>Chat Cleared by Admin</b></span>')");
print "You cleared the chat.";
}

Link to comment
Share on other sites


<?php
if ($view == clearc) {
$chat_user = array();
$chat_data = array();
$sql = "SELECT * FROM chat;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
	$chat_user[] .= $row['user'];
	$chat_log[] .= $row['chat'];
}
$count = 0;
$file_data = "";
foreach ($chat_user as $value){
	$file_data .= $value.":".$chat_log[$count]."\n";
	$count++;
}
$file = "log_".date("Y-m-D").".txt"; //automatically genrates a new file named something like
									//log_2008-01-12.txt
$directory ="path/to/saves/"; //must be writeable change it to where you want the logs to be saved.
$handle = fopen ($directory.$file, "w+");
fwrite ($handle, $file_data);
fclose($handle);
mysql_query("delete from chat");
mysql_query("insert into chat (id, user, chat) values('1', '<span style=color:#FF0000>Admin</span>', '<span style=color:#FF0000><b>Chat Cleared by Admin</b></span>')");
print "You cleared the chat.";
}
?>

the script will get all the info in your db, and dump it into a file, then it will clear the db and post what you normally post. Simple as that

Link to comment
Share on other sites

Warning: fopen(path/to/saves/log_2008-04-Wed.txt) [function.fopen]: failed to open stream: No such file or directory in /home/monk3h/public_html/admin.php on line 180

 

Warning: fwrite(): supplied argument is not a valid stream resource in /home/monk3h/public_html/admin.php on line 181

 

Warning: fclose(): supplied argument is not a valid stream resource in /home/monk3h/public_html/admin.php on line 182

You cleared the chat.

 

 

 

o.O

Link to comment
Share on other sites

what is cleared is cleared. the script is linear, meaning that step1->step2->step3->

I have it written so that it does this:

first, it gets all the data, and dumps it in 2 arrays, then it outputs that data into a variable, then writes that variable into a file, then clears the table.

If it's writing only that data, it's because that's the only thing there.

Link to comment
Share on other sites

Even after spamming my Chat it still just Logs the same thing. Are you sure its not logging after it deletes? I understand what your saying but there is no reason for it to log just 1 line of chat, especialy just the first line. :S

Link to comment
Share on other sites

this should fix that issue (if not, let me know)

<?php
if ($view == clearc) {
$chat_user = array();
$chat_data = array();
$sql = "SELECT * FROM chat;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
	$chat_user[] = $row['user'];
	$chat_log[] = $row['chat'];
}
$count = 0;
$file_data = "";
foreach ($chat_user as $value){
	$file_data .= $value.":".$chat_log[$count]."\n";
	$count++;
}
$file = "log_".date("Y-m-d").".txt"; //automatically genrates a new file named something like
									//log_2008-01-12.txt
$directory ="path/to/saves/"; //must be writeable change it to where you want the logs to be saved.
$handle = fopen ($directory.$file, "w+");
fwrite ($handle, $file_data);
fclose($handle);
mysql_query("delete from chat");
mysql_query("insert into chat (id, user, chat) values('1', '<span style=color:#FF0000>Admin</span>', '<span style=color:#FF0000><b>Chat Cleared by Admin</b></span>')");
        print "You cleared the chat.<br />";
        print "DATA IN NEW FILE:\n".$file_data."<br />";
        print "data stored in <a href='".$directory.$file."'>".$directory.$file."</a>";
}
?>

Link to comment
Share on other sites

<?php
if ($view == clearc) {
$chat_user = array();
$chat_data = array();
$sql = "SELECT * FROM chat;";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)){
	$chat_user[] = $row['user'];
	$chat_log[] = $row['chat'];
}
$count = 0;
$file_data = "";
foreach ($chat_user as $value){
	$file_data .= $value.":".$chat_log[$count]."\n";
	$count++;
}
$file = "log_".date("Y-m-d_Gi_s").".txt"; //automatically genrates a new file named something like
									//log_2008-01-12.txt
$directory ="path/to/saves/"; //must be writeable change it to where you want the logs to be saved.
$handle = fopen ($directory.$file, "w+");
fwrite ($handle, $file_data);
fclose($handle);
mysql_query("delete from chat");
mysql_query("insert into chat (id, user, chat) values('1', '<span style=color:#FF0000>Admin</span>', '<span style=color:#FF0000><b>Chat Cleared by Admin</b></span>')");
        print "You cleared the chat.<br />";
        print "DATA IN NEW FILE:\n".$file_data."<br />";
        print "data stored in <a href='".$directory.$file."'>".$directory.$file."</a>";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.