Jump to content

Recommended Posts

This is a small part of the code but it must be enough to explain the problem.

 

foreach($uniqueWords as $word) {
$word = quote_smart($word);
// first check if the word is in the exclude list
// if it is then go to the next word
$result = mysql_query("SELECT * FROM historer_excludewords WHERE word='$word'");
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) {
	continue;
}

// count how many times the word occurs
$wordcount = $valueCountArray[$word];
//echo $word."\t".$wordcount."<br/>";

// check if word exist in historer_words
$wordid;

$result = mysql_query("SELECT * FROM historer_words WHERE word='$word' LIMIT 1");
print_r2($result);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) { // the word exists
  // get the id
}
else { // the word doesn't exist
 // echo "don't exist<br/>";
 // add the word to the database and get the id of it
 $query = "INSERT INTO historer_words VALUES ('', '$word')";
 //$result = insertQuery($query);
}

}

 

print_r2 looksl ike:

function print_r2($val){
echo "<pre>";
print_r($val);
echo "</pre>";
}

 

The problem is here:

	$result = mysql_query("SELECT * FROM historer_words WHERE word='$word' LIMIT 1");
print_r2($result);
$num_rows = mysql_num_rows($result);
if ($num_rows > 0) { // the word exists
  // get the id
}

 

the historer_words only contains unique word, that's why i use LIMIT 1.

However, after i know it exists i would like to get the id of it (as in the auto-increment value).

 

$result prints stuff like:

Resource id #1288

 

But i have no clue what kind of id that is since my tables are still empty atm.

So how could i get the id if it existed?

Link to comment
https://forums.phpfreaks.com/topic/250048-get-the-id/
Share on other sites

You should (almost) never execute queries inside of loops. Assuming that you want to -

 

1) Remove any words that are in historer_excludewords

 

2) Add any new words to historer_words

 

3) Get the id of the words

 

The following would be close to what your logic should be doing -

 

<?php

$words = $uniqueWords; // get a copy of the words (in case the original in $uniqueWords is used later)

$find = array_map('quote_smart',$words); // apply function to each element of the array

// eliminate excluded words
$query = "SELECT word FROM historer_excludewords WHERE word IN('".implode("','",$find)."')";
$result = mysql_query($query);
$found = array();
while($row = mysql_fetch_assoc($result)){
$found[] = $row['word'];
}

$words = array_diff($words,$found); // remove any excluded words

// insert any 'new' words. assumes that the word column is an index. the IGNORE keyword will ignore duplicate key errors for words that are already present.
$insert = array_map('quote_smart',$words); // quote the remaining words
$query = "INSERT IGNORE INTO historer_words (word) VALUES ('".implode("'),('",$insert)."')";
mysql_query($query);

// get the id for each new/existing word
$query = "SELECT id,word FROM historer_words WHERE word IN('".implode("','",$insert)."')";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
// your code to get the id for each word from the $uniqueWords array that is (new or existing) in the historer_words table
}

 

Link to comment
https://forums.phpfreaks.com/topic/250048-get-the-id/#findComment-1283261
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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