I cannot join two tables in php

Asked

Viewed 50 times

0

good afternoon, I have the following problem, I am developing a project and I need to join two tables of mysql to appear in the table made in php. I mean, I want the following: inserir a descrição da imagem aqui

this table above has to receive information from the users table and from another table. I have tried everything but the table always appears empty. I have the following code

<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(strlen($_SESSION['alogin'])==0)
    {   
header('location:index.php');
}
else{
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        
        <!-- Title -->
        <title>CSSJD | Página Inicial</title>
        
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
        <meta charset="UTF-8">
        <meta name="description" content="Responsive Admin Dashboard Template" />
        <meta name="keywords" content="admin,dashboard" />
        <meta name="author" content="Steelcoders" />
        
        <!-- Styles -->
        <link type="text/css" rel="stylesheet" href="../assets/plugins/materialize/css/materialize.min.css"/>
        <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">    
        <link href="../assets/plugins/metrojs/MetroJs.min.css" rel="stylesheet">
        <link href="../assets/plugins/weather-icons-master/css/weather-icons.min.css" rel="stylesheet">

        	
        <!-- Theme Styles -->
        <link href="../assets/css/alpha.min.css" rel="stylesheet" type="text/css"/>
        <link href="../assets/css/custom.css" rel="stylesheet" type="text/css"/>
        
    </head>
    <body>

        <?php include('includes/header.php');?>
            
       <?php include('includes/sidebar.php');?>

            <main class="mn-inner">
                <div class="middle-content">
                    <div class="row no-m-t no-m-b">
                    <div class="col s12 m12 l4">
                        <div class="card stats-card">
                            <div class="card-content">
                            
                            
                                <span class="card-title">Total Utentes</span>
    <?php
$sql = "SELECT id from tblutentes";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$dptcount=$query->rowCount();
?>                            
                                <span class="stats-counter"><span class="counter"><?php echo htmlentities($dptcount);?></span></span>
                            </div>
                            <div id="sparkline-line"></div>
                        </div>
                    </div>
                    <div class="col s12 m12 l4">
                        <div class="card stats-card">
                            <div class="card-content">
                                <span class="card-title">Total PIIs Preenchidos</span>
                                    <?php
$sql = "SELECT id from  tblpii";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$leavtypcount=$query->rowCount();
?>   
                                <span class="stats-counter"><span class="counter"><?php echo htmlentities($leavtypcount);?></span></span>
                      
                            </div>
                            <div class="progress stats-card-progress">
                                <div class="determinate" style="width: 70%"></div>
                            </div>
                        </div>
                    </div>
                </div>
                 
                    <div class="row no-m-t no-m-b">
                        <div class="col s12 m12 l12">
                            <div class="card invoices-card">
                                <div class="card-content">
                                 
                                    <span class="card-title">Últimos Utentes Preenchidos</span>
                             <table id="example" class="display responsive-table ">
                                    <thead>
                                        <tr>
                                            <th width="100">ID</th>
                                            <th width="100">Nome Utente</th>
                                            <th width="100">Número Utente</th>  
                                        </tr>
                                    </thead>
                                 
                                    <tbody>
                                    <?php $sql = "SELECT tblpii.NomeUtente as nomeutente,tblutentes.* from tblutentes join tblpii on tblpii.id=tblutentes.id order by lid desc limit 3";
                                    $query = $dbh -> prepare($sql);
                                    $query->execute();
                                    $results=$query->fetchAll(PDO::FETCH_OBJ);
                                    $cnt=1;
                                    if($query->rowCount() > 0)
                                    {
                                    foreach($results as $result)
                                    {  ?>   
                                 <tr>
                                            <td><?php echo $cnt;?>.</td>
                                            <td><?php echo $row['nomeutente'];?></td>
                                            <td><?php echo $row['nrutente'];?></td>
                                            <td><a href="manageleavetype.php?manageleavetype=<?php echo htmlentities($result->lid);?>" class="waves-effect waves-light btn blue m-b-xs"  > Ver Detalhes </a></td>
                                    </tr>
                                        
                                    </tbody>
                                </table>
<?php }} ?>
                                </div>
                            </div>
                        </div>
                    </div>
              
            </main>
          
</div>
        

1 answer

0

Have you tried using INNER JOIN ? EX:

"SELECT * FROM c_endereco INNER JOIN c_cliente ON c_cliente.idunico_cliente = c_endereco.idunico_cliente INNER JOIN c_cadastro ON c_cadastro.idunico_cliente = c_endereco.idunico_cliente WHERE c_endereco.idunico_cliente='$clienteid';"

You can do with as many tables as you want, if you don’t understand very well a researched on Inner Join has a lot of tutorial!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.