Mysql - Inner Join runs on PHPADMIN but not on php SCRIPT

Asked

Viewed 81 times

1

I have a small PHP script to interact with MAKERIOD. When I run select in PHPADMIN I get the return : "Showing 0 - 16 records (total 17, 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'

However I want to run this SELECT within a . PHP passing as parameters the CPF and the ACADEMIC year. But when I put to execute http://localhost/lernotes.php gives out time ; I’ve changed this time limit( what I don’t think is correct ) but returns nothing.

//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);
    }
}

?>

  • Which version of php is installed?

  • Xampp 3.22 Apache/2.4.17 (Win32) Openssl/1.0.2d PHP/5.5.33 Database client version: libmysql - mysqlnd 5.0.11 PHP Version 5.5.33

  • Se eu troco aquele SELECT por esse $sqlBusca = 'SELECT Alunos.cnomealu,Alunos.ccpf,Tbdiario.cn1,Tbdiario.cn2,Tbdiario.cn3 FROM alunos INNER JOIN tbdiario ON Alunos.ncodaluno=Tbdiario.ncodaluno WHERE Alunos.ccpf =13922054765'; funciona .... but I need the ANOLETIVO field and therefore several INNER JOIN

  • Could be a problem with Casesensitive, depending on the OS installed.

  • Win 7 original .

  • Change your SQL to look right as tables and columns are created in the database. I think Phpmyadmin already treats it to disregard.

  • Diario - fk ncodalu , Diario - fk ccodementa , Tbementa - fk ncodtur e Tbementa - fk ncodmat é assim...

  • Sorry, Marcondes, what do you mean "change your SQL to be right as the tables and columns are created in the database." ?

  • Staff, I checked and saw that I had to change the order of the tables : I was DIARIO-EMENTA-MATERIA-ALUNOS-TURMA-MATERIA , in order to show the name of the student , the subject ( discipline ) and the respective grades.

Show 4 more comments

1 answer

0

I checked and saw that I had to change the order of the tables : I was DIARIO-EMENTA-MATERIA-STUDENTS-CLASS-CLASS and changed to DIARIO- ALUNOS-EMENTA-TURMA-MATERIA , with the aim of showing the student’s name ( discipline ) and the respective grades. I run in the browser and I get the return ; however when I enter the field Tbmateria.cnomemat ( discipline name ) the return is the empty browser ( with nothing and the message "Select Empty" ) Any field of the table Tbmateria I can return , less 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 : Observem o campo tbmateria.ncodmat , retorna OK

Changing only the field tbmateria.ncodmat pot tbmateria.cnomemat ( is what interests me ) :

$sqlBusca  = 'SELECT Tbmateria.cnomemat, 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 ';

Result , empty ... nor entered the ELSE dealing with the empty return : Vazio sem mensagem de erro

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

Browser other questions tagged

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