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?
– Oralista de Sistemas
Saying that it is not working is very comprehensive. What is the error/result you are getting and what was expected?
– Gabriel Heming
then the problem is that nothing is displayed on the screen, would have to display the result with a JSON formatting.
– abduzeedo