Hi, I have gt another problem. Firstly, you see these codes;
$myFile = "testFile.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "New Stuff 1
";
fwrite($fh, $stringData);
$stringData = "New Stuff 2
";
fwrite($fh, $stringData);
fclose($fh);
So bascially this codes could actually append a file and the output in the .txt file would be:
New Stuff1
New Stuff2
am i correct to say that, right?
So is there any way that I could input SQL instructions so that the text file would display the value of my, erm, my SQL codes?
So if I were to say, INSERT name WHERE table =1 and the .txt file would have the 'name'?
It would actually be "New Stuff1 New Stuff2 ". The text file would only contain one single line. And there's no WHERE clause in an INSERT statement. The code to write text into a file can be simpler with the file_put_contents() function if you happen to be using PHP 5 (see example below). If you want to query the database and save the output into a text file, it would be something like this:
Database
Table: some_table
id - user - score
1 - Dokuro - 12345
2 - Zakuro - 2345
3 - Sakura - 345
Application
<?php
$db = new mysqli('localhost', 'database_username', 'database_password', 'name_of_database'); // establish a database connection
$result = $db->query("SELECT user FROM some_table WHERE id = 1"); // query the database
$row = $result->fetch_array(); // retrieves a row from the query result as an array
$name = $row['user']; // get the value stored in the "user" column from the row retrieved
for ($i = 1; $i <= 4; $i++) { // writes the name 4 times in the file
file_put_contents('filename.text', $name . "\r\n", FILE_APPEND); // "\r\n" is used to "push" any following text onto the next line
}
?>
Resulting Text File (filename.text)
Dokuro
Dokuro
Dokuro
Dokuro
Is there any easier examples based on the codes below??
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = "SELECT name,type,MIN(totalprice) FROM transactions GROUP BY name,type";
$result = mysql_query($query) or die(mysql_error());
$myFile = "testFile.txt"; //Specific Flie
$fh = fopen($myFile, 'a') or die("can't open file"); //open file
$stringData = "New Stuff 1 ";
fwrite($fh, $stringData);
$stringData = "THE SQL COMMAND CODES MAYBE $query ???"; // HELP NEEDED HERE
fwrite($fh, $stringData);
$stringData = "New Stuff 2 ";
fwrite($fh, $stringData);
fclose($fh);
Originally posted by Trcyng:Is there any easier examples based on the codes below??
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = "SELECT name,type,MIN(totalprice) FROM transactions GROUP BY name,type";
$result = mysql_query($query) or die(mysql_error());$myFile = "testFile.txt"; //Specific Flie
$fh = fopen($myFile, 'a') or die("can't open file"); //open file
$stringData = "New Stuff 1 ";
fwrite($fh, $stringData);$stringData = "THE SQL COMMAND CODES MAYBE $query ???"; // HELP NEEDED HERE
fwrite($fh, $stringData);
$stringData = "New Stuff 2 ";
fwrite($fh, $stringData);
fclose($fh);
I can't tell what you are trying to achieve from your code. Why are you storing the SQL query string in the text file? And there's no function call to fetch the data from $result.
ok you see...
SInce
$myFile = "testFile.txt"; // is specific the file
$fh = fopen($myFile, 'a') or die("can't open file"); //open
$stringData = "New Stuff 1 "; //write in (This is I am trying to put data here from database)
fwrite($fh, $stringData);
$stringData = "New Stuff 2 "; //write in (This is I am trying to put data here from database)
fwrite($fh, $stringData);
fclose($fh);
Originally posted by Trcyng:ok you see...
SInce
$myFile = "testFile.txt"; // is specific the file
$fh = fopen($myFile, 'a') or die("can't open file"); //open
$stringData = "New Stuff 1 "; //write in (This is I am trying to put data here from database)
fwrite($fh, $stringData);
$stringData = "New Stuff 2 "; //write in (This is I am trying to put data here from database)
fwrite($fh, $stringData);
fclose($fh);
Just a question before we continue, are you developing on PHP 4 or PHP 5?
PHP 5
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = "SELECT name,type,MIN(totalprice) FROM transactions GROUP BY name,type";
$result = mysql_query($query) or die(mysql_error());
$data = '';
while ($row = mysql_fetch_array($result) {
$data .= 'Name: ' . $row['name'] . ', Type: ' . $row['type'] . "
";
}
$myFile = "testFile.txt";
file_put_contents($myFile, $data, FILE_APPEND);
?>
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = "SELECT name,type,MIN(totalprice) FROM transactions GROUP BY name,type";
$result = mysql_query($query) or die(mysql_error());
$data = '';
while ($row = mysql_fetch_array($result) {
$data .= 'Name: ' . $row['name'] . ', Type: ' . $row['type'] . " ";
}
$myFile = "testFile.txt";
file_put_contents($myFile, $data, FILE_APPEND);
?>
Based on your codes righht? If I want to like display this
Example this is my highest data: (Meaning that this is what I want to add as static)
43 (Meaning that this is my SQL codes which the values are dynamic)
How can I actually do it based on the codes (Static)
Originally posted by Trcyng:<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = "SELECT name,type,MIN(totalprice) FROM transactions GROUP BY name,type";
$result = mysql_query($query) or die(mysql_error());
$data = '';
while ($row = mysql_fetch_array($result) {
$data .= 'Name: ' . $row['name'] . ', Type: ' . $row['type'] . " ";
}
$myFile = "testFile.txt";
file_put_contents($myFile, $data, FILE_APPEND);
?>
Based on your codes righht? If I want to like display this
Example this is my highest data: (Meaning that this is what I want to add as static)
43 (Meaning that this is my SQL codes which the values are dynamic)
How can I actually do it based on the codes (Static)
I don't get your question. Do you mean the text to be written into the file is hardcoded in the PHP code instead of being pulled out from a MySQL database? In that case, the code will simply be:
<?php
$text = 'New Stuff 123';
$myFile = "testFile.txt";
// the line below writes the value in $text into the file specified in $myFile
// if the file already exists, it will NOT overwrite the file, but instead append the text to the end of the file
file_put_contents($myFile, $text, FILE_APPEND);
?>
from database itself.
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error
connecting to mysql');
mysql_select_db($dbname);
$query = "SELECT name,type,MIN(totalprice) FROM transactions GROUP
BY name,type";
$result = mysql_query($query) or die(mysql_error());
$data = '';
while ($row = mysql_fetch_array($result) {
$data .= 'Name: ' . $row['name'] . ', Type: ' . $row['type'] . "
";
}
$myFile = "testFile.txt";
file_put_contents($myFile, $data, FILE_APPEND);
?>
Based on your codes righht? If I want to like display this;
------------------------------------------
This is SGFORUMS: (Meaning that this is what I want to add as static)
43 (Meaning that this is my SQL codes which the values are dynamic)
people is viewing this (Static)
-----------------------------------------
The words in bold is my question
<?php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
$query = "SELECT name,type,MIN(totalprice) FROM transactions GROUP BY name,type";
$result = mysql_query($query) or die(mysql_error());
$data = "This is sgForums.\r\n";
while ($row = mysql_fetch_array($result) {
$data .= 'Name: ' . $row['name'] . ', Type: ' . $row['type'] . "\r\n";
}
$data .= "People will be looking at this text file.\r\n";
$myFile = "testFile.txt";
file_put_contents($myFile, $data, FILE_APPEND);
?>
then what about file overwrite? thanks for your help!!!!
Originally posted by Trcyng:then what about file overwrite? thanks for your help!!!!
Change the line from:
file_put_contents($myFile, $data, FILE_APPEND);
to
file_put_contents($myFile, $data);