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?

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);
?>

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.";
}


<?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

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

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.

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>";
}
?>

<?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>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.