Jump to content

Improving Performance....


drbigfresh

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.