Gnub Posted February 15, 2007 Share Posted February 15, 2007 Im trying to put several table results into a simple .txt file. However im getting an error which will be explained below. There are approximately...20-30 of fields to be put in. Im attempting to have the .txt file look similar to a CVS file, so comma's and the like. so at the end of the first record, i want a new line, and starts a new record...rince and repeat. I was getting the results, however, after each record, a new line is not started, and continues off for a long while, before starting a new line, without me asking. (on notepad) Currently im getting an error after i've tried a different approach, but that didn't work. Warning: Wrong parameter count for fputs() in ..URL../CVSGen.php on line 41. line 41, is the last part of the fputs(). "); fputs($file, "\n['], .$row[Row_No]." , ".$row[Offer_ID]." , ".$row[Total]." , ".$row[Rating]"); The fopen, and all the others are present. Is there an easier way? or can i get a fix for this? Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Maybe this will help your situation: <?php fputs($file, "\n['], .$row[Row_No]." , ".$row[Offer_ID]." , ".$row[Total]." , ".$row[Rating]." , "); ?> See any problem with your fputs now? Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 Sorry Bal, just noticed the error i wrote in my first message. But i've ammended the post. Sorry for the post error. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Even still, you're passing a ton of extra parameters to fputs. Your Error: "\n['], .$row[Row_No]." I believe you meant: "\n['], ".$row[Row_No]." With it as it was it could actually cut off the string after $row[Row_No]. then place a comma seperating arguments. Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 Ok, cheers for that B! I managed to solve that small error. Splitting the Fputs() into smaller pieces, there isn't as much strain on the function. now it gives me some other error.... Parse error: syntax error, unexpected ']' in URL Line # fputs($file, ".$row[Operator]." , ".$row[PublicNote]." , ".$row." , ".$row." , ".$row[include]." However, this part is identical to the others, appart from the obvious column name, i've singled the part out in red as the problem...can you see anything i cant? Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Why are you starting with ".$row[Operator]." ? Syntax highlight it again: fputs($file, ".$row[Operator]." , ".$row[PublicNote]." , ".$row[Email]." , ".$row[url]." , ".$row[include]." You're still pushing way too many vars into fputs. You need to fix up your string first. Post up all of the fputs code for this particular area. Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 How many am i allowed to get away with? at most? fputs($file, ".$row[Operator]." , ".$row[PublicNote]." , ".$row." , ".$row." , ".$row[include]." , ".$row[CCC]." , ".$row[Tod]." , ".$row[bCC]." , ".$row[Amex]." , ".$row[CDW]." , ".$row[bookingFee]." , ".$row[ABTA]." , ".$row[ATOL]." , ".$row[OfferID]." , ".$row[OtherBond]." , ".$row[OfferLink]." , ".$row[AccRef]." , ".$row[NAME]." , ".$row[Hotel]." , ".$row[supplier]." , ".$row[Flights]." , ".$row[ACCOM]." , ".$row[Total]"); Hope this helps. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 fputs() accepts 3 arguments. The file handle must be the first, the string must be the second, and you can optionally include a length. <?php fputs($file, ".$row[Operator]." , ".$row[PublicNote]." , ".$row[Email]." , ".$row[url]." , ".$row[include]." , ".$row[CCC]." , ".$row[Tod]." , ".$row[bCC]." , ".$row[Amex]." , ".$row[CDW]." , ".$row[bookingFee]." , ".$row[ABTA]." , ".$row[ATOL]." , ".$row[OfferID]." , ".$row[OtherBond]." , ".$row[OfferLink]." , ".$row[AccRef]." , ".$row[NAME]." , ".$row[Hotel]." , ".$row[supplier]." , ".$row[Flights]." , ".$row[ACCOM]." , ".$row[Total]"); ?> Your problem is how you start it. Even though strings start with quotes, starting with a variable means that you skip the quotes. As well, you don't need them at the end either. <?php fputs($file, $row[Operator]." , ".$row[PublicNote]." , ".$row[Email]." , ".$row[url]." , ".$row[include]." , ".$row[CCC]." , ".$row[Tod]." , ".$row[bCC]." , ".$row[Amex]." , ".$row[CDW]." , ".$row[bookingFee]." , ".$row[ABTA]." , ".$row[ATOL]." , ".$row[OfferID]." , ".$row[OtherBond]." , ".$row[OfferLink]." , ".$row[AccRef]." , ".$row[NAME]." , ".$row[Hotel]." , ".$row[supplier]." , ".$row[Flights]." , ".$row[ACCOM]." , ".$row[Total]); ?> That should fix the string problem. Now for your other problem, include is a reserved keyword. You cannot use it, as PHP will always give you errors. You're going to have to rename your include field to something else. As you can see in the syntax highlight above, Include shows up as green, indicating that it's a reserved keyword. Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 Thanks for that B! however, i now have a big spanner in the works...is there any other way of doing this without renaming fields? Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 I take back what I said about include. The way you're using it, yes it will be taken as their reserved keyword, however the way you're doing it is bad practice. With the way you're doing it now, it first checks to see what's in the [] to see if it's a defined symbol (any function names, keywords, etc.), and if it's not it takes it as a string literal. Relying on this is defined as a bad practice in the PHP manual itself. What you want to do is access the fields using a single quoted string, like so: <?php fputs($file, $row['Operator']." , ".$row['PublicNote']." , ".$row['Email']." , ".$row['URL']." , ".$row['Include']." , ".$row['CCC']." , ".$row['Tod']." , ".$row['BCC']." , ".$row['Amex']." , ".$row['CDW']." , ".$row['BookingFee']." , ".$row['ABTA']." , ".$row['ATOL']." , ".$row['OfferID']." , ".$row['OtherBond']." , ".$row['OfferLink']." , ".$row['AccRef']." , ".$row['NAME']." , ".$row['Hotel']." , ".$row['Supplier']." , ".$row['Flights']." , ".$row['ACCOM']." , ".$row['Total']); ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 15, 2007 Share Posted February 15, 2007 A better way of doing this (IMHO) would be to put all the values you want in the comma separated string into a temporary array and then use implode to put the commas in when you write the record.. Also, I always put the "\n" at the end of the record not at the beginning. <?php $fields = array('Operator','PublicNote','Email','URL','Include','CCC','Tod','BCC','Amex','CDW','BookingFee','ABTA','ATOL','OfferID', 'OtherBond','OfferLink','AccRef','NAME','Hotel','Supplier','Flights','ACCOM','Total'); $tmp = array(); foreach($fields as $fld) $tmp[] = $row[$fld]; fputs($file,implode(',',$tmp) . "\n"); ?> By putting the fields you want to write out in an array, it's very easy to add another value. Ken Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 will look at both methods now. cheers gents. Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 Kudo's to you both. Both seem to work, but i must have screwed up somewhere, none of the DB's fields are appearing on the .txt file. We'll use B's option as an example. but both seem to deliver when working to the fputs(). <?php $sql = "SELECT * from <Table> Limit 0,100;"; $db = mysql_connect("N/A", "User", "Pass"); mysql_select_db("User",$db); $result = mysql_query($sql,$db) or die(mysql_error()); while($row = mysql_fetch_object($result)) { $filename = "todo.txt"; touch("$filename"); $file = fopen("$filename", "a"); fputs($file, $row['Operator']." , ".$row['PublicNote']." , ".$row['Email']." , ".$row['URL']." , ".$row['Include']." , ".$row['CCC']." , ".$row['Tod']." , ".$row['BCC']." , ".$row['Amex']." , ".$row['CDW']." , ".$row['BookingFee']." , ".$row['ABTA']." , ".$row['ATOL']." , ".$row['OfferID']." , ".$row['OtherBond']." , ".$row['OfferLink']." , ".$row['AccRef']." , ".$row['NAME']." , ".$row['Hotel']." , ".$row['Supplier']." , ".$row['Flights']." , ".$row['ACCOM']." , ".$row['Total']); fclose($file); ?> What i get as a return is: , , , , , , , , , , , , , , ,/n Connection variables, and all are correct, as they are in use in other scripts. thanks for your patience guys. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 It's the LIMIT statement in your query. You're limiting you results to returning 0 rows. LIMIT Syntax: LIMIT num_to_return,offset Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 urgh, i hate brain farts.... Thanks again B! Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 Now get nothing moved into my .txt file. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Instead of mysql_fetch_object use mysql_fetch_array. Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 no luck still. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 What does your query look like now? Also, before you use $row try doing a print_r($row);, and looking at the data it spits out. Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 no data shown when using Print_r($row); $sql = "SELECT * from <table> LIMIT 100,0;"; Im using a limiter, for the moment, as the table runs a little bigger. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Then that means that you're not getting any data. As horrible as this may sound, do you actually have data in the database? Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 yes, it just had another update, and i've just checked, and there are data in the DB. Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Alright, next step in the process, are you sure your table name is correct? Also, did you directly insert print_r($row)? If you did, and you aren't storing your data in $row, then it's not going to work. Quote Link to comment Share on other sites More sharing options...
Gnub Posted February 15, 2007 Author Share Posted February 15, 2007 I've inserted print_r($row); as the first line that is read when inside the WHILE{} and table name is correct Quote Link to comment Share on other sites More sharing options...
Balmung-San Posted February 15, 2007 Share Posted February 15, 2007 Hmm, then it should print out at least something. Are you still using mysql_fetch_object, or mysql_fetch_array? Also, do: echo gettype($row); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.