MySql - inner join runs in PHPADMIN but in SCRIPT php does not

1

I have a small PHP script to interact with MAKERIOD. When I execute the select in PHPADMIN I have the return: "Showing records from 0 - 16 (17 total, Query took 8.0945 seconds.)"

SELECT Tbdiario.cn1, Tbdiario.cn2, Tbdiario.cn3, Tbdiario.cn4, Alunos.cnomealu, Tbmateria.cnomemat FROM tbdiario INNER JOIN tbementa ON Tbdiario.ccodementa = Tbementa.ccodementa INNER JOIN tbmateria ON Tbementa.ncodmat = Tbmateria.ncodmat INNER JOIN alunos ON Tbdiario.ncodaluno = Alunos.ncodaluno INNER JOIN tbturma ON Tbementa.ncodtur = Tbturma.ncodtur WHERE Alunos.ccpf = '13922054765' AND Tbturma.cletivo = '2009'

But I want to run this SELECT within a .PHP by passing the CPF and the LEARNING year as parameters. But when I put to run link it gives out time; I have already altered this time limit (which I do not think is correct) but does not return anything.

//include_once 'conexao.php';
require "Dbig.class.php";
Header('Content-Type: application/json');
//$c_cpf = $_POST['c_cpf']  ;
//$c_anoletivo = $_POST['c_anoletivo'];
$db = new Db();
$dados=array();

$sqlBusca = 'SELECT Tbdiario.cn1, Tbdiario.cn2, Tbdiario.cn3, Tbdiario.cn4,';
$sqlBusca .=' Alunos.cnomealu, Tbmateria.cnomemat FROM tbdiario ';
$sqlBusca .=' INNER JOIN tbementa ON  Tbdiario.ccodementa = Tbementa.ccodementa ';
$sqlBusca .=' INNER JOIN tbmateria ON  Tbementa.ncodmat = Tbmateria.ncodmat ';
$sqlBusca .=' INNER JOIN alunos  ON  Tbdiario.ncodaluno = Alunos.ncodaluno ';
$sqlBusca .=' INNER JOIN tbturma ON  Tbementa.ncodtur = Tbturma.ncodtur ';
$sqlBusca .=' WHERE  Alunos.ccpf = "13922054765" ';


$resultado = $db->query($sqlBusca);
if ($resultado)
{
    while ($array = mysqli_fetch_assoc($resultado)) 
    {
        $dados[] = $array;
    }
    echo json_encode($dados);
}     
else{
    echo "VAZIO !!!";
}   

? >

<?php

/**
*  conexão com o banco de dados usando mysqli
*/
class Db{

    private $con;

    // Faz a conexão com o banco assim que o objeto é criado
    function __construct(){
        $this->con = mysqli_connect('localhost', 'root', '', 'dbcadastro');
        if (mysqli_connect_errno($this->con)) {
            echo "Problemas para conectar no banco. Verifique os dados!";
            die();
        }
    }

    // Faz a consulta sql
    public function query($sql){
        return mysqli_query($this->con, $sql);
    }
}

? >

    
asked by anonymous 30.07.2018 / 19:08

1 answer

0

I checked and saw that I had to change the order of the tables: I was DIARY-EMPLOYMENT-MATERIAL-STUDENTS-TURMA and I changed to DAILY- STUDENTS-EMENT-TURMA-MATTER, with the purpose of showing the student's name, discipline) and the respective notes. I run in the browser and have the return; however when I enter the field Tbmateria.cnomemat (name of the course) the return is the empty browser (without nothing nor the message of "Select Empty") Any field of the Tbmateria table I can return, except what I want. $sqlBusca = 'SELECT Tbmateria.ncodmat, Alunos.cnomealu, Tbdiario.cn1, Tbdiario.cn2, Tbdiario.cn3, Tbdiario.cn4 FROM '; $sqlBusca .=' tbdiario INNER JOIN alunos ON Alunos.ncodaluno = Tbdiario.ncodaluno '; $sqlBusca .=' INNER JOIN tbementa ON Tbdiario.ccodementa = Tbementa.ccodementa '; $sqlBusca .=' INNER JOIN tbmateria ON Tbmateria.ncodmat = Tbementa.ncodmat '; $sqlBusca .=' INNER JOIN tbturma ON Tbementa.ncodtur = Tbturma.ncodtur'; $sqlBusca .=' WHERE Alunos.ccpf= "13922054765" Order by tbmateria.cnomemat ';

The return is Json:

Changingonlythefieldtbmateria.ncodmatpottbmateria.cnomemat(that'swhatinterestsme):

$sqlBusca='SELECTTbmateria.cnomemat,Alunos.cnomealu,Tbdiario.cn1,Tbdiario.cn2,Tbdiario.cn3,Tbdiario.cn4FROM';$sqlBusca.='tbdiarioINNERJOINalunosONAlunos.ncodaluno=Tbdiario.ncodaluno';$sqlBusca.='INNERJOINtbementaONTbdiario.ccodementa=Tbementa.ccodementa';$sqlBusca.='INNERJOINtbmateriaONTbmateria.ncodmat=Tbementa.ncodmat';$sqlBusca.='INNERJOINtbturmaONTbementa.ncodtur=Tbturma.ncodtur';$sqlBusca.='WHEREAlunos.ccpf="13922054765" Order by tbmateria.cnomemat ';

Result, empty ... nor entered the ELSE that handles the empty return:

Any field in the TBMATERIA table (ncodmat, cabrev and ctipo) Select returns the right Json, only the cnomemat field does. If I add in the WHERE clause, Tbmateria.ncodmat = 77 (a random code) Select returns the correct records.

    
31.07.2018 / 14:00