INNER JOIN Mysql error and JSON view

Asked

Viewed 226 times

0

Hello

I need to display the results of a SELECT with INNER JOIN in JSON format, my query is working, because I tested inside PHPMYADMIN, but my page does not work, I have tested everything, what can it be ? Someone helps me find the mistake ? It must be something very simple.

My SQL

    -- phpMyAdmin SQL Dump
    -- version 4.6.4
    -- https://www.phpmyadmin.net/
    --
    -- Host: 127.0.0.1
    -- Generation Time: May 30, 2017 at 01:45 PM
    -- Server version: 5.7.14
    -- PHP Version: 5.6.25

    SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
    SET time_zone = "+00:00";


    /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
    /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
    /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
    /*!40101 SET NAMES utf8mb4 */;

    --
    -- Database: `projeto001`
    --

    -- --------------------------------------------------------

    --
    -- Table structure for table `compras`
    --

    CREATE TABLE `compras` (
      `id` int(100) NOT NULL,
      `username` varchar(100) NOT NULL,
      `projeto` varchar(100) NOT NULL,
      `produto` varchar(100) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `compras`
    --

    INSERT INTO `compras` (`id`, `username`, `projeto`, `produto`) VALUES
    (1, 'Fulano', 'Projeto Novo', 'produto01');

    -- --------------------------------------------------------

    --
    -- Table structure for table `empresa`
    --

    CREATE TABLE `empresa` (
      `id` int(100) NOT NULL,
      `username` varchar(100) NOT NULL,
      `projeto` varchar(100) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `empresa`
    --

    INSERT INTO `empresa` (`id`, `username`, `projeto`) VALUES
    (1, 'Fulano', 'Projeto Novo');

    -- --------------------------------------------------------

    --
    -- Table structure for table `estoque`
    --

    CREATE TABLE `estoque` (
      `id` int(100) NOT NULL,
      `produto` varchar(100) NOT NULL,
      `quantidade` varchar(100) NOT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

    --
    -- Dumping data for table `estoque`
    --

    INSERT INTO `estoque` (`id`, `produto`, `quantidade`) VALUES
    (1, 'produto01', '100');

    --
    -- Indexes for dumped tables
    --

    --
    -- Indexes for table `compras`
    --
    ALTER TABLE `compras`
      ADD PRIMARY KEY (`id`);

    --
    -- Indexes for table `empresa`
    --
    ALTER TABLE `empresa`
      ADD PRIMARY KEY (`id`);

    --
    -- Indexes for table `estoque`
    --
    ALTER TABLE `estoque`
      ADD PRIMARY KEY (`id`);

    --
    -- AUTO_INCREMENT for dumped tables
    --

    --
    -- AUTO_INCREMENT for table `compras`
    --
    ALTER TABLE `compras`
      MODIFY `id` int(100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
    --
    -- AUTO_INCREMENT for table `empresa`
    --
    ALTER TABLE `empresa`
      MODIFY `id` int(100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
    --
    -- AUTO_INCREMENT for table `estoque`
    --
    ALTER TABLE `estoque`
      MODIFY `id` int(100) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
    /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
    /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
    /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

My Code

    <?php

    $projeto = $_GET['projeto'];

    //PDO is a extension which defines a lightweight, consistent interface for accessing databases in PHP.
    $db=new PDO('mysql:dbname=mydatabase;host=localhost;','username','password');
    //here prepare the query for analyzing, prepared statements use less resources and thus run faster
    $nome = addslashes(filter_input(INPUT_GET, 'nome', FILTER_SANITIZE_SPECIAL_CHARS));

    //Produtos
    $row=$db->prepare("SELECT empresa.username, empresa.projeto, compras.username, compras.projeto, compras.produto, estoque.produto FROM `empresa` AS empresa LEFT JOIN `compras` AS compras ON compras.`username` = empresa.`username` AND compras.`projeto` = empresa.`projeto` LEFT JOIN `estoque` AS estoque ON compras.`produto` = estoque.`produto` WHERE empresa.projeto = 'Projeto Novo' and compras.username = 'Fulano'");

    $row->execute();//execute the query
    $json_data=array();//create the array
    foreach($row as $rec)//foreach loop
    {
    $json_array['empresa.username']=$rec['empresa.username'];
    $json_array['empresa.projeto']=$rec['empresa.projeto'];
    $json_array['compras.username']=$rec['compras.username'];
    $json_array['compras.projeto']=$rec['compras.projeto'];
    $json_array['compras.produto']=$rec['compras.produto'];
    $json_array['estoque.produto']=$rec['estoque.produto'];
    //here pushing the values in to an array
    array_push($json_data,$json_array);

    }

    //built in PHP function to encode the data in to JSON format
    echo json_encode($json_data);
    ?>

Valew

  • What error you get?

  • Saying that it is not working is very comprehensive. What is the error/result you are getting and what was expected?

  • then the problem is that nothing is displayed on the screen, would have to display the result with a JSON formatting.

2 answers

0

Hello, put before json_encode the following code:

header('Content-Type: application/json');

This serves for the answer to be in json format, without this, your answer is considered a web page.

Note: You cannot have any content output before setting the header, if you have already output some content, you will receive a php error/warning message.

  • So, my code is working because I have already tested with a simple SELECT, but now nothing appears using SELECT with INNER JOIN, maybe the error is time to display the data on the screen.

  • It is likely that you are getting some PHP error. Add these lines at the beginning of the code to display them: ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); With this, we can get a better idea of the error that is occurring.

  • There was no mistake.

  • of a var_dump($json_data) and say what returns.

0


Try this code, I made a change, added the $lines variable and fetchall.

<?php

//Produtos
$row=$db->prepare("SELECT empresa.username, empresa.projeto, compras.username, compras.projeto, compras.produto, estoque.produto FROM `empresa` AS empresa LEFT JOIN `compras` AS compras ON compras.`username` = empresa.`username` AND compras.`projeto` = empresa.`projeto` LEFT JOIN `estoque` AS estoque ON compras.`produto` = estoque.`produto` WHERE empresa.projeto = 'Projeto Novo' and compras.username = 'Fulano'");

$row->execute();
$json_data=array();
$results = $row->fetchAll();

foreach($results as $rec)
{
    $json_array['username']=$rec['username'];
    $json_array['projeto']=$rec['projeto'];
    $json_array['produto']=$rec['produto'];

    //here pushing the values in to an array
    array_push($json_data,$json_array);
}

//built in PHP function to encode the data in to JSON format
header('Content-Type: application/json');
echo json_encode($json_data);
  • I tested the code but it didn’t work, the problem is that the array is coming empty, will be that the problem is in SELECT, maybe the simple quotes ?

  • abduzeedo, have to paste the SQL of your base, I replicate and I can solve. A question, when you paste the SQL that is there above directly in the database and runs, returns some result?

  • I edited the code and inserted my SQL to create the database, I think that makes it easier to help me. Thanks

  • Hello Bruno. Alright ? What will be the mistake ?

  • Updated the code, did the import from your base and fixed the code, is now functional.

  • Obs, I’ve removed the connection string. The most important thing that was missing is "$Results = $Row->fetchAll();", without this line, you were trying to read the contents of the query string and not the object returned from the query. Also the columns of the returned array were incorrect. The column name is not accompanied by the table name, unless you put an alias for each column with the names that were previously.

Show 2 more comments

Browser other questions tagged

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