期望参数1是资源

期望参数1是资源

本文介绍了Mysql_fetch_array()期望参数1是资源,布尔值在(重复)中给出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮忙.. !!我很难修复我的代码。



我尝试过:



原始代码

please help..!! i am tired to fix my code.

What I have tried:

original code

function jumlah_data($kondisi){

        //sql

        if($kondisi==''){

            $sql = mysql_query("SELECT COUNT(*) FROM data_training $kondisi"); 

        }else{

            $sql = mysql_query("SELECT COUNT(*) FROM data_training WHERE $kondisi");                       

        }      

        $row = mysql_fetch_array($sql); // line 42 error

        $jml = $row['0'];

        return $jml;

    }



重建


rebuild

//fungsi menghitung jumlah data
  function jumlah_data($kondisi){
    //sql
    if($kondisi==''){
      $query = "SELECT COUNT(*) FROM data_training $kondisi";
      $sql = mysql_query($query) || 
     die("Error SQL: $query". PHP_EOL .mysql_error().");
      //$sql = mysql_query("SELECT COUNT(*) FROM data_training $kondisi");  
    }else{
      $query = "SELECT COUNT(*) FROM data_training WHERE $kondisi";
      $sql = mysql_query($query) || 
      die("Error SQL: $query". PHP_EOL .mysql_error().");
      //$sql = mysql_query("SELECT COUNT(*) FROM data_training WHERE $kondisi");
    }    
    $row = mysql_fetch_array($sql);  
    $jml = $row['0'];
    return $jml;
  } 



但是错误


But error

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\XAMPP\htdocs\MMM\New folder\fungsi.php on line 48

LEAF
Error SQL: SELECT COUNT(*) FROM data_training WHERE AND target='laku'
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right sy





完整代码



Full code

?php
	//fungsi cek nilai atribut
	function cek_nilaiAtribut($field , $kondisi){
		//sql disticnt		
		$hasil = array();
		if($kondisi==''){
			$sql = mysql_query("SELECT DISTINCT($field) FROM data_training");					
		}else{
			$sql = mysql_query("SELECT DISTINCT($field) FROM data_training WHERE $kondisi");					
		}
		$a=0;
		while($row = mysql_fetch_array($sql)){
			$hasil[$a] = $row['0'];
			$a++;
		}	
		return $hasil;
	}	
	//fungsi cek heterogen data
	function cek_heterohomogen($field , $kondisi){
		//sql disticnt
		if($kondisi==''){
			$sql = mysql_query("SELECT DISTINCT($field) FROM data_training");					
		}else{
			$sql = mysql_query("SELECT DISTINCT($field) FROM data_training WHERE $kondisi");					
		}
		//jika jumlah data 1 maka homogen
		if (mysql_num_rows($sql) == 1) {                      
			$nilai = "homogen";
		}else{
			$nilai = "heterogen";
		}		
		return $nilai;
	}	
	//fungsi menghitung jumlah data
	function jumlah_data($kondisi){
		//sql
		if($kondisi==''){
			$sql = mysql_query("SELECT COUNT(*) FROM data_training $kondisi");	
		}else{
			$sql = mysql_query("SELECT COUNT(*) FROM data_training WHERE $kondisi");						
		}		
		$row = mysql_fetch_array($sql);	
		$jml = $row['0'];
		return $jml;
	}
	//fungsi menghitung gain
	function hitung_gain($kasus , $atribut , $ent_all , $kondisi1 , $kondisi2 , $kondisi3 , $kondisi4){
		$data_kasus = '';
		if($kasus!=''){
			$data_kasus = $kasus." AND ";
		}
		//untuk atribut 2 nilai atribut	
		if($kondisi3==''){
			$j_laku1 = jumlah_data("$data_kasus target='laku' AND $kondisi1");
			$j_tidak1 = jumlah_data("$data_kasus target='tidak' AND $kondisi1");
			$jml1 = $j_laku1 + $j_tidak1;
			$j_laku2 = jumlah_data("$data_kasus target='laku' AND $kondisi2");
			$j_tidak2 = jumlah_data("$data_kasus target='tidak' AND $kondisi2");
			$jml2 = $j_laku2 + $j_tidak2;
			//hitung entropy masing-masing kondisi
			$jml_total = $jml1 + $jml2;
			$ent1 = hitung_entropy($j_laku1 , $j_tidak1);
			$ent2 = hitung_entropy($j_laku2 , $j_tidak2);
			$gain = $ent_all - ((($jml1/$jml_total)*$ent1) + (($jml2/$jml_total)*$ent2));
		}
		//untuk atribut 3 nilai atribut
		else if($kondisi4==''){
			$j_laku1 = jumlah_data("$data_kasus target='laku' AND $kondisi1");
			$j_tidak1 = jumlah_data("$data_kasus target='tidak' AND $kondisi1");
			$jml1 = $j_laku1 + $j_tidak1;
			$j_laku2 = jumlah_data("$data_kasus target='laku' AND $kondisi2");
			$j_tidak2 = jumlah_data("$data_kasus target='tidak' AND $kondisi2");
			$jml2 = $j_laku2 + $j_tidak2;
			$j_laku3 = jumlah_data("$data_kasus target='laku' AND $kondisi3");
			$j_tidak3 = jumlah_data("$data_kasus target='tidak' AND $kondisi3");
			$jml3 = $j_laku3 + $j_tidak3;
			//hitung entropy masing-masing kondisi
			$jml_total = $jml1 + $jml2 + $jml3;
			$ent1 = hitung_entropy($j_laku1 , $j_tidak1);
			$ent2 = hitung_entropy($j_laku2 , $j_tidak2);
			$ent3 = hitung_entropy($j_laku3 , $j_tidak3);			
			$gain = $ent_all - ((($jml1/$jml_total)*$ent1) + (($jml2/$jml_total)*$ent2) 
						+ (($jml3/$jml_total)*$ent3));				
		}
		//desimal 3 angka dibelakang koma
		$gain = round($gain,3);	
		if($gain>0){
			echo "Gain ".$atribut." = ".$gain."<br>";
		}		
		mysql_query("INSERT INTO gain VALUES ('','$atribut','$gain')");
	}
	//fungsi menghitung entropy
	function hitung_entropy($nilai1 , $nilai2){
		$total = $nilai1 + $nilai2;
		//jika salah satu nilai 0, maka entropy 0
		if($nilai1==0 or $nilai2==0){
			$entropy = 0;
		}else{
			$entropy = (-($nilai1/$total)*(log(($nilai1/$total),2))) + (-($nilai2/$total)*(log(($nilai2/$total),2)));
		}		
		//desimal 3 angka dibelakang koma
		$entropy = round($entropy, 3);	
		return $entropy;
	}
	//fungsi hitung rasio
	function hitung_rasio($kasus , $atribut , $gain , $nilai1 , $nilai2 , $nilai3){				
		$data_kasus = '';
		if($kasus!=''){
			$data_kasus = $kasus." AND ";
		}
		//menentukan jumlah nilai
			$jmlNilai=3;
			//jika nilai 3 kosong maka nilai atribut-nya 2
			if($nilai3==''){
				$jmlNilai=2;
			}					
		mysql_query("TRUNCATE rasio_gain");		
		if($jmlNilai==3){
			$opsi11 = jumlah_data("$data_kasus ($atribut='$nilai2' OR $atribut='$nilai3')");
			$opsi12 = jumlah_data("$data_kasus $atribut='$nilai1'");
			$tot_opsi1=$opsi11+$opsi12;
			$opsi21 = jumlah_data("$data_kasus ($atribut='$nilai3' OR $atribut='$nilai1')");
			$opsi22 = jumlah_data("$data_kasus $atribut='$nilai2'");
			$tot_opsi2=$opsi21+$opsi22;
			$opsi31 = jumlah_data("$data_kasus ($atribut='$nilai1' OR $atribut='$nilai2')");
			$opsi32 = jumlah_data("$data_kasus $atribut='$nilai3'");
			$tot_opsi3=$opsi31+$opsi32;			
			//hitung split info
			$opsi1 = (-($opsi11/$tot_opsi1)*(log(($opsi11/$tot_opsi1),2))) + (-($opsi12/$tot_opsi1)*(log(($opsi12/$tot_opsi1),2)));
			$opsi2 = (-($opsi21/$tot_opsi2)*(log(($opsi21/$tot_opsi2),2))) + (-($opsi22/$tot_opsi2)*(log(($opsi22/$tot_opsi2),2)));
			$opsi3 = (-($opsi31/$tot_opsi3)*(log(($opsi31/$tot_opsi3),2))) + (-($opsi32/$tot_opsi3)*(log(($opsi32/$tot_opsi3),2)));
			//desimal 3 angka dibelakang koma
			$opsi1 = round($opsi1,3);
			$opsi2 = round($opsi2,3);
			$opsi3 = round($opsi3,3);										
			//hitung rasio
			$rasio1 = $gain/$opsi1;
			$rasio2 = $gain/$opsi2;
			$rasio3 = $gain/$opsi3;
			//desimal 3 angka dibelakang koma
			$rasio1 = round($rasio1,3);
			$rasio2 = round($rasio2,3);
			$rasio3 = round($rasio3,3);
			//cetak
			echo "Opsi 1 : <br>jumlah ".$nilai2."/".$nilai3." = ".$opsi11.
				"<br>jumlah ".$nilai1." = ".$opsi12.
				"<br>Split = ".$opsi1.
				"<br>Rasio = ".$rasio1."<br>";
			echo "Opsi 2 : <br>jumlah ".$nilai3."/".$nilai1." = ".$opsi21.
				"<br>jumlah ".$nilai2." = ".$opsi22.
				"<br>Split = ".$opsi2.
				"<br>Rasio = ".$rasio2."<br>";
			echo "Opsi 3 : <br>jumlah ".$nilai1."/".$nilai2." = ".$opsi31.
				"<br>jumlah ".$nilai3." = ".$opsi32.
				"<br>Split = ".$opsi3.
				"<br>Rasio = ".$rasio3."<br>";
				
			//insert 
			mysql_query("INSERT INTO rasio_gain VALUES 
						('' , 'opsi1' , '$nilai1' , '$nilai2 , $nilai3' , '$rasio1'),
						('' , 'opsi2' , '$nilai2' , '$nilai3 , $nilai1' , '$rasio2'),
						('' , 'opsi3' , '$nilai3' , '$nilai1 , $nilai2' , '$rasio3')");
		}
		$sql_max = mysql_query("SELECT MAX(rasio_gain) FROM rasio_gain");
		$row_max = mysql_fetch_array($sql_max);	
		$max_rasio = $row_max['0'];
		$sql = mysql_query("SELECT * FROM rasio_gain WHERE rasio_gain=$max_rasio");
		$row = mysql_fetch_array($sql);	
		$opsiMax = array();
		$opsiMax[0] = $row[2];
		$opsiMax[1] = $row[3];		
		echo "<br>=========================<br>";
		return $opsiMax;		
	}
?>

推荐答案




这篇关于Mysql_fetch_array()期望参数1是资源,布尔值在(重复)中给出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 13:17