Jump to content

Trying to insert a query into a text file


BadGoat

Recommended Posts

Hello!

 

I am trying to save a MSSQL query to a text file, and having no luck. I've read tutorials on how to create a text file and how to write some text to it:

 

$ourFileName = "test.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Heres line number 1!\n";
fwrite($fh, $stringData);
$stringData = "Heres line number 2!\n";
fwrite($fh, $stringData);
fclose($fh);

 

And I have written the query which displays the information I want, but I am not sure how to implement the query into the text file creation.

 

Query:

$sqlquery = "SELECT text.*, text_text.* FROM text_text, text WHERE text_text.textid = text.textID AND text.textID = '".$row['textID']."' ORDER BY dep_text_id";
$queryresult = mssql_query($sqlquery) or die(" Could not execute mssql query !");
$row = mssql_fetch_row($queryresult);

 

Looking for a bit of direction!

you are fetching a row and it will output the row like an array so you need to decide if you will put the array or a specific column in the database, I'll say here that you want only a column from the database.

this is not a 100% correct but try it and tell me :)

 

//this part checks if the file exists so you don't need it
$ourFileName = "test.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

//this part creates or/and puts the information to the file
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");

$sqlquery = "SELECT text.*, text_text.* FROM text_text, text WHERE text_text.textid = text.textID AND text.textID = '".$row['textID']."' ORDER BY dep_text_id";
$queryresult = mssql_query($sqlquery) or die(" Could not execute mssql query !");
$row = mssql_fetch_row($queryresult);

fwrite($fh, $row[""TheNameOfTheColumn]);
fclose($fh);

 

I think this won't help you because I've never used mssql but it's owrth a try  ;D

I tried a handful of different syntaxes (I get them mixed up, still to this day)

 

// write form results to file
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

$myFile = "testFile.txt";
$page_number = $row['page_number'];
$line_number = $row['line_number'];
$deo_text = $row['deo_text'];
$fh = fopen($myFile, 'w') or die("can't open file");

fwrite($fh, $row['page_number']);
fclose($fh);

It does not error out, but it does write a blank text file.

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.