I have a counter which takes it value from a .txt file - This value comes from the sum of a MySQL field - This script gets that value all I need to do is write it to the .txt??
<?php
// Make a MySQL Connection
$host="localhost";//hostname
$username="root";//username
$password="";//db_password
$db_name="db_name";//database name
//connect to database
mysql_connect("$host","$username","$password")or die("cannot connect to server");
mysql_select_db("$db_name")or die("cannot select database");
$query = "SELECT SUM(field) FROM table";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo $row['SUM(field)'];
echo "<br />";
}
?>
PHP writing to .txt file
Moderator: General Moderators
Re: PHP writing to .txt file
Why do you want to write the same values to a .txt when you have it stored in a db?
-
Rusty_Bad_Robot
- Forum Newbie
- Posts: 4
- Joined: Mon Mar 15, 2010 7:53 am
Re: PHP writing to .txt file
I have my flash counter working from that .txt file - I could link it to the db but i've chosen to go this route.
Re: PHP writing to .txt file
Ok, then all you need to do is to write that data to the file yes.
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: PHP writing to .txt file
Code: Select all
file_put_contents('/path/to/your/file.txt', $data);mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
-
Rusty_Bad_Robot
- Forum Newbie
- Posts: 4
- Joined: Mon Mar 15, 2010 7:53 am
Re: PHP writing to .txt file
Yeh - I have it working now... although I now need it to overwirite the file contents instead of just adding more information?
// Get Result
while($row = mysql_fetch_array($result)){
$filename = 'counter.txt';
$somecontent = $row['SUM(field)'];
}
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
// Get Result
while($row = mysql_fetch_array($result)){
$filename = 'counter.txt';
$somecontent = $row['SUM(field)'];
}
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
-
Rusty_Bad_Robot
- Forum Newbie
- Posts: 4
- Joined: Mon Mar 15, 2010 7:53 am
Re: PHP writing to .txt file
o.k just me being silly - had to change the mode for fopen.
Thanks for your help.
-Peace out -
Thanks for your help.
-Peace out -