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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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.