Home > Back-end >  inner joining multiple table but mysql_query is not working
inner joining multiple table but mysql_query is not working

Time:12-29

<div >
    <?php
    $Nama=mysqli_real_escape_string($conn,$_GET['title']);
    //$sql="select * from peserta where Nama='$Nama'";
    $sql = "select peserta.ID_Peserta,hakim.ID_Hakim,acara.ID_Badminton,admin.ID_Admin 
                from (((peserta 
                inner join hakim on peserta.ID_Hakim=hakim.ID_Hakim)
                inner join acara on peserta.ID_Badminton=acara.ID_Badminton)
                inner join admin on peserta.ID_Admin=admin.ID_Admin"; 
    $searchresult=mysqli_query($conn,"select * from peserta where Nama='$Nama'",$sql);
    $queryResults=mysqli_num_rows($searchresult);

    if($queryResults>0){
        while($row=mysqli_fetch_assoc($searchresult)){
            echo"<div class='article-box'>
            <h3>".$row['Nama']."</h3>
            <p>".$row['ID_Peserta']."</p>
            <p>".$row['noKP']."</p>
            <p>".$row['Kelas']."</p>
            <p>".$row['Keseluruhan_Markah']."</p>
            <p>".$row['Ranking']."</p>
            <p>".$row['ID_Hakim']."</p>
            <p>".$row['ID_Badminton']."</p>
            <p>".$row['ID_Admin']."</p>

            </div>";
        }
        
    }else{
        echo "There are no results matching your search!";
    }

been looking forward to linking 3 different tables using the ID into my 'peserta' table but there is an error:

Fatal error: Uncaught TypeError: mysqli_query(): Argument #3 ($result_mode) must be of type int, string given in D:\Other\xampp\htdocs\project\article.php:25 Stack trace: #0 D:\Other\xampp\htdocs\project\article.php(25): mysqli_query(Object(mysqli), 'select * from p...', 'select peserta....') #1 {main} thrown in D:\Other\xampp\htdocs\project\article.php on line 25

this is a school project of mine and im new to php so any help will be appreciated

CodePudding user response:

As a guess from looking at what I think you are trying to do based upon the question title and the incorrect structuring of the sql query perhaps the following is appropriate?

<div >
    <?php
        if( isset( $_GET['title'] ) ){
            $sql='select 
                p.ID_Peserta,
                p.Nama,
                p.noKP,
                p.Kelas,
                p.Keseluruhan_Markah,
                p.Ranking,
                h.ID_Hakim,
                a.ID_Badminton,
                ad.ID_Admin 
            from peserta p
                inner join hakim h on p.ID_Hakim=h.ID_Hakim
                inner join acara a on p.ID_Badminton=a.ID_Badminton
                inner join admin ad on p.ID_Admin=ad.ID_Admin
            where p.Nama=?';


            $stmt=$conn->prepare( $sql );
            $stmt->bind_param( 's', $_GET['title'] );
            $stmt->execute();
            $stmt->bind_result( $idp, $nama, $nokp, $kelas, $kesmar, $rank, $idh, $idb, $ida );

            while( $stmt->fetch() )printf(
                '<div >
                    <h3>%s</h3>
                    <p>%s</p>
                    <p>%s</p>
                    <p>%s</p>
                    <p>%s</p>
                    <p>%s</p>
                    <p>%s</p>
                    <p>%s</p>
                    <p>%s</p>
                </div>', 
                $nama, $idp, $nokp, $kelas, $kesmar, $rank, $idh, $idb, $ida );
            
            $stmt->free_result();
            $stmt->close();
            
        }
    ?>
</div>

To test that the SQL query returns a result try running the following in either the GUI or from the mySQL commandline:

select 
    p.ID_Peserta,
    p.Nama,
    p.noKP,
    p.Kelas,
    p.Keseluruhan_Markah,
    p.Ranking,
    h.ID_Hakim,
    a.ID_Badminton,
    ad.ID_Admin 
from peserta p
    inner join hakim h on p.ID_Hakim=h.ID_Hakim
    inner join acara a on p.ID_Badminton=a.ID_Badminton
    inner join admin ad on p.ID_Admin=ad.ID_Admin
where p.Nama="<<< SUBSTITUTE A TITLE HERE >>>"

for instance:

select 
    p.ID_Peserta,
    p.Nama,
    p.noKP,
    p.Kelas,
    p.Keseluruhan_Markah,
    p.Ranking,
    h.ID_Hakim,
    a.ID_Badminton,
    ad.ID_Admin 
from peserta p
    inner join hakim h on p.ID_Hakim=h.ID_Hakim
    inner join acara a on p.ID_Badminton=a.ID_Badminton
    inner join admin ad on p.ID_Admin=ad.ID_Admin
where p.Nama="banana" <----- the title/$_GET['title'] value appears in your db???
  • Related