Jump to content

select db using array key value


bulz

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/259239-select-db-using-array-key-value/
Share on other sites

 

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];
         }

      }

   }

 

@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?

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.