BadGoat Posted May 14, 2008 Share Posted May 14, 2008 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 https://forums.phpfreaks.com/topic/105622-trying-to-insert-a-query-into-a-text-file/ Share on other sites More sharing options...
shedokan Posted May 14, 2008 Share Posted May 14, 2008 I don't understand you do you want to save $sqlquery to a file or something from the database to a file? Link to comment https://forums.phpfreaks.com/topic/105622-trying-to-insert-a-query-into-a-text-file/#findComment-541137 Share on other sites More sharing options...
BadGoat Posted May 14, 2008 Author Share Posted May 14, 2008 Sorry for confusion That's correct, I want to save the output from this query to a text file. Link to comment https://forums.phpfreaks.com/topic/105622-trying-to-insert-a-query-into-a-text-file/#findComment-541141 Share on other sites More sharing options...
shedokan Posted May 14, 2008 Share Posted May 14, 2008 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 Link to comment https://forums.phpfreaks.com/topic/105622-trying-to-insert-a-query-into-a-text-file/#findComment-541149 Share on other sites More sharing options...
BadGoat Posted May 14, 2008 Author Share Posted May 14, 2008 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 https://forums.phpfreaks.com/topic/105622-trying-to-insert-a-query-into-a-text-file/#findComment-541166 Share on other sites More sharing options...
shedokan Posted May 15, 2008 Share Posted May 15, 2008 is the variable $row is set? Link to comment https://forums.phpfreaks.com/topic/105622-trying-to-insert-a-query-into-a-text-file/#findComment-541741 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.