bulz Posted March 19, 2012 Share Posted March 19, 2012 Hi guyz, please help me I want to get an id from my database, I use a word to search it which is stored as keys of array. But all the keys can't be found in my database, even though it's there. Here some parts of my code $arrKeys = array_keys($arrResult); foreach($arrKeys as $key) { //if($arrResult[$key]>1) { echo $key/*."=>". $arrResult[$key]*/."<br>"; //$q = mysql_query("select id_katadasar from tb_katadasar where katadasar='".$key."'"); $kon->query("select id_katadasar from tb_katadasar where katadasar='".$key."'"); var_dump($kon->query); //echo "select id_katadasar from tb_katadasar where katadasar='".$key."'"; //$jum = $kon->getJumlah(); //$row = mysql_fetch_array($q, MYSQL_ASSOC) or die(mysql_error() . ''. $q); //echo count($row); if($row = $kon->tampilkan()) //if($row = mysql_fetch_array($q, MYSQL_NUM)) { //$res = $kon->tampilkan(); echo $row[0]; } //else { //echo "tidak ada di db <br><br>"; } } } $kon->query is equal to mysql_query() $kon->tampilkan return $row=mysql_fetch_array(query) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/259239-select-db-using-array-key-value/ Share on other sites More sharing options...
freelance84 Posted March 19, 2012 Share Posted March 19, 2012 It would be handy to see a print_r of the of $arrResult. But what does something like this print out.. foreach($arrResult as $key => $value) { if($key>1) { echo $key; $kon->query(" select id_katadasar from tb_katadasar where katadasar='".$key."' "); var_dump($kon->query); if($row = $kon->tampilkan()) { echo $row[0]; } } } Quote Link to comment https://forums.phpfreaks.com/topic/259239-select-db-using-array-key-value/#findComment-1328979 Share on other sites More sharing options...
bulz Posted March 19, 2012 Author Share Posted March 19, 2012 Thanks for reply Actually, I will do something inside the if.. But, in my example, I would like to check, which one is exist in my database, which is not.. so, I try to echo the "id", if the word is exist in my database.. Quote Link to comment https://forums.phpfreaks.com/topic/259239-select-db-using-array-key-value/#findComment-1329054 Share on other sites More sharing options...
creata.physics Posted March 19, 2012 Share Posted March 19, 2012 @freelance - You've removed array_keys() from the code meaning not every key will necessarily be an integer, so the if statement would fail each time there was a key string. Using count() is a nice an easy way to avoid this issue, because even if the array key was a string or not, it'd be counted since it just counts the keys. if ( count($key) > 1 ) Also to further help, I do need to see the contents of $arrResult. Before you start the foreach loop, can you add print_r($arrResult); and give us what it outputs bulz? Quote Link to comment https://forums.phpfreaks.com/topic/259239-select-db-using-array-key-value/#findComment-1329097 Share on other sites More sharing options...
bulz Posted March 19, 2012 Author Share Posted March 19, 2012 $arrResult is a result from stemming file, which is an array with key. The keys are words, with value of frequency of specific words appear in that file. Example: key(string) => value(int) hello=> 20 php=>15 freaks=>9 forum=>8 Quote Link to comment https://forums.phpfreaks.com/topic/259239-select-db-using-array-key-value/#findComment-1329246 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.