ayok Posted December 6, 2009 Share Posted December 6, 2009 Hi, I have an output like this: john,18,Cancer How can I change this to 'john','18','Cancer' with php? Thanks in advanced, ayok Quote Link to comment https://forums.phpfreaks.com/topic/184162-insert-quotes/ Share on other sites More sharing options...
the-botman Posted December 6, 2009 Share Posted December 6, 2009 not sure what u trying to do with this but a simple str_replace should do the trick Quote Link to comment https://forums.phpfreaks.com/topic/184162-insert-quotes/#findComment-972312 Share on other sites More sharing options...
c-o-d-e Posted December 6, 2009 Share Posted December 6, 2009 Whats your PHP for this, It'll give a whole lot better idea. Quote Link to comment https://forums.phpfreaks.com/topic/184162-insert-quotes/#findComment-972315 Share on other sites More sharing options...
ayok Posted December 6, 2009 Author Share Posted December 6, 2009 Hi, thanks. I am trying to make a csv importer. And the output "1,john,18,Cancer" is from the csv file. However, when I tried to insert the data into a table, i kept getting mysql error, and the way to fix it is to insert quotes on the data, like " '1','john','18','Cancer' ". Here is my php so far: $filecontents = file ("testable.csv"); for($i=1; $i<sizeof($filecontents); $i++) { $insertrecord = "INSERT INTO ".$tablename." (".$filecontents[0].") VALUES (". $filecontents[$i].")"; } If I echo the $insertrecord i got INSERT INTO testable (id,name,age,zodiac) VALUES (1,john,18,Cancer) which return mysql error. It works when I changed the values into '1','john','18','Cancer'. Quote Link to comment https://forums.phpfreaks.com/topic/184162-insert-quotes/#findComment-972319 Share on other sites More sharing options...
PFMaBiSmAd Posted December 6, 2009 Share Posted December 6, 2009 In the code you originally posted, then removed when you edited the post, you were using implode() to get your current string. All you would need to do is alter the impload() to add single quotes next to the comma and then add some single-quotes to the start and end of the complete string. Since I don't know what variables you were using in your original code, take a look at this - <?php $output = "john,18,Cancer"; $together = implode("','",explode(',',$output)); $output = "'$together'"; echo $output; ?> Quote Link to comment https://forums.phpfreaks.com/topic/184162-insert-quotes/#findComment-972346 Share on other sites More sharing options...
c-o-d-e Posted December 10, 2009 Share Posted December 10, 2009 I'm confused as to why your wanting to echo a query? The thing with the code you stated, it's hard to change $filecontents[$i] to have ' ' around each string. You'd have to get the strings in that way. If $filecontents[$1] meant "Hello" by doing echo "'". $filecontents[$1] ."'"; should do the trick. You'd have to show where $filecontents[$1] is defined, which would be where $1 is defined in the file testable.csv I think. Quote Link to comment https://forums.phpfreaks.com/topic/184162-insert-quotes/#findComment-974724 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.