drbigfresh Posted April 21, 2007 Share Posted April 21, 2007 I am parsing out some words and dumping them into a database using the below code. The way I have it now, we're executing an insert on the database for each word (everytime it loops through strtok). I know that in sql I could just build the query using a new line (\n) and then execute the SQL once after the loop...but I can't seem to find a similar way of getting it to work in MySQL. Is it somehow possible or could someone point me in the right direction.... Thanks! Scott. $string = $item['description']; $tok = strtok($string, " "); while ($tok !== false) { $tok = mysql_escape_string($tok); if (strlen($tok) > 2 && strlen($tok) < 100) { $query = "insert into words(word, userid) value('$tok', '".$db->qstr($matches[1])."')"; $result = $db->Execute($query) or print $db->ErrorMsg() . ""; } $tok = strtok(" "); } Link to comment https://forums.phpfreaks.com/topic/47984-improving-performance/ Share on other sites More sharing options...
trq Posted April 21, 2007 Share Posted April 21, 2007 You might want to take a look at your $db class. Maybe it has some support for multiple queries. MySql does, but mysql_query (php) does not. Link to comment https://forums.phpfreaks.com/topic/47984-improving-performance/#findComment-234474 Share on other sites More sharing options...
corbin Posted April 21, 2007 Share Posted April 21, 2007 You could do one giant query.... But, if there's a lot of values for $tok then it literally could be giant haha. while($tok !=== false) { if($strlen($tok) > 2 && strlen($tok) < 100) { $tok_array[] = "('{$tok}', '" . $db->qstr($matches[1]) . "')"; } } $values = implode($tok, ","); $q = "INSERT INTO words(word, userid) VALUES {$values}"; $db->Execute($q); Link to comment https://forums.phpfreaks.com/topic/47984-improving-performance/#findComment-234495 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.