clankill3r Posted October 29, 2011 Share Posted October 29, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/250048-get-the-id/ Share on other sites More sharing options...
Pikachu2000 Posted October 29, 2011 Share Posted October 29, 2011 It's a query result resource. mysql_query() returns a result resource that must then be mysql_fetch_*'d from to use the data within it. Quote Link to comment https://forums.phpfreaks.com/topic/250048-get-the-id/#findComment-1283255 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2011 Share Posted October 29, 2011 Also, there's no point in using LIMIT 1. All that does is consume more processor cycles parsing the sql and applying the limit to a single row in the result set. Quote Link to comment https://forums.phpfreaks.com/topic/250048-get-the-id/#findComment-1283258 Share on other sites More sharing options...
PFMaBiSmAd Posted October 29, 2011 Share Posted October 29, 2011 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 } Quote Link to comment https://forums.phpfreaks.com/topic/250048-get-the-id/#findComment-1283261 Share on other sites More sharing options...
clankill3r Posted November 3, 2011 Author Share Posted November 3, 2011 good to now, thanks Quote Link to comment https://forums.phpfreaks.com/topic/250048-get-the-id/#findComment-1284456 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.