Select and unlink file on 3 tables - php

Please help me to check my query. I have search a lot and I have'nt try to select 3 tables before.
I think I got it right but I dont know why there's nothing happen.
public function delSection($delete_id)
{
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt->execute(array(":del_id"=>$delete_id));
while($linkRow=$stmt->fetch(PDO::FETCH_ASSOC))
{
unlink(__DIR__."/Admin/cover_images/".$linkRow['sec_cover']);
unlink(__DIR__."/Admin/Files/".$linkRow['sec_id']."/".$linkRow['file_name']);
rmdir(__DIR__."/Admin/Files/".$linkRow['sec_id']);
}
$stmt2 = $this->conn->prepare("DELETE tbl_section, tbl_login, tbl_content FROM tbl_section
JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:del_id");
$stmt2->bindparam(":del_id",$delete_id);
$stmt2->execute();
return true;
}
What I am trying to do is to select * from 3 tables and fetch their data with fk sec_id
here's the manual running of query
link:
Code:

Done With LEFT OUTER JOIN QUERY
$stmt = $this->conn->prepare("SELECT * FROM tbl_section
LEFT OUTER JOIN tbl_login ON (tbl_login.sec_id = tbl_section.sec_id)
LEFT OUTER JOIN tbl_content ON (tbl_content.sec_id = tbl_section.sec_id)
WHERE tbl_section.sec_id=:unlink_id");

Related

PHP, How to display the 2nd table

DBOperations.php
public function getByQRID($id){
$stmt = $this->conn->prepare("SELECT students.*, courses.* FROM students INNER JOIN courses ON courses.id = students.course_id WHERE students.id = ?");
$stmt->bind_param("s", $id);
$stmt->execute();
return $stmt->get_result()->fetch_assoc();
}
Update the code
If you are you using MySql, then try this:
select * from Student
inner join Course on Student.course_id = Course.id
where Student.id = 1
(1 it's an example, and I'm guessing things, since your question wasn't entirely clear).
I hope it helps!

How to display data from database table independently

Problem desc.
I have databese called demo. In this databse i have columns. Two of them are otk and zamestnanci.
/*zamestnanci = employees*/
So i have some data in otk table:
/*otk columns: id_otk|ciarovy_kod|cislo_zakazky|zamestnanci|*/
/*id_otk = autoincrement*/
/*zamestnanci can be only number*/
INSERT INTO otk (ciarovy_kod, cislo_zakazky, zamestnanci) VALUES ('65464', '564', '1');
And now i have some data in zamestnanci:
/*zamestnanci columns: id_zamestnanci|titul|meno|titulz|*/
/*id_zamestnanci = autoincrement*/
INSERT INTO zamestnanci (titul, meno, titulz) VALUES ('ads', 'John', 'das');
And now here is my code for displaing and searching data from otk:
if(isset($_GET['hladat']))
{
$hladatHodnotu = $_GET['hladatHodnotu'];
// hladat v setkych stlpcoch
// pouzitie concat funkcie pre vyhladanie iba urciteho stlpca
$sql = "SELECT * FROM otk JOIN zamestnanci ON zamestnanci.id_zamestnanci=otk.zamestnanci
JOIN zariadenia ON zariadenia.id=otk.zariadenie
JOIN stav ON stav.id=otk.stav
JOIN technologie ON technologie.id=otk.technologie
JOIN obrazky ON obrazky.id_obrazky=otk.obrazok
WHERE CONCAT(`ciarovy_kod`) LIKE '%".$hladatHodnotu."%'";
$vysledokHladania = filtrovatTabulku($sql);
}
//Zobrazovanie dat z viacerych tabuliek do jednej
/* JOIN zamestnanci ON zamestnanci.id_zamestnanci=otk.zamestnanci
JOIN zariadenia ON zariadenia.id=otk.zariadenie
JOIN stav ON stav.id=otk.stav
JOIN technologie ON technologie.id=otk.technologie
JOIN obrazky ON obrazky.id_obrazky=otk.obrazok*/
else {
$sql = "SELECT * FROM otk JOIN zamestnanci ON zamestnanci.id_zamestnanci=otk.zamestnanci
JOIN zariadenia ON zariadenia.id=otk.zariadenie
JOIN stav ON stav.id=otk.stav
JOIN technologie ON technologie.id=otk.technologie
JOIN obrazky ON obrazky.id_obrazky=otk.obrazok";
//$sql = "SELECT * FROM zariadenia, otk WHERE zariadenia.id=otk.zariadenie";
$vysledokHladania = filtrovatTabulku($sql);
}
// funkcia na pripojenie a spustenie $sql
function filtrovatTabulku($sql)
{
//Zahrnut pripojenie k db
include'../db/dbinfo.php';
$vysledokHladania = mysqli_query($con, $sql);
return $vysledokHladania;
}
And now when i empty table "zamestnanci" it will not display data from "otk" with id of that "zamestnanci" because he do not exist. So i want to ask you if there is way how to display if "zamestnanci" is not exist.
You're using the wrong JOIN.
You should learn how each join works before using them, read more about them here http://www.sql-join.com/sql-join-types/

Inner Join more than 2 tables (5 tables)

Before you guys send me to something else because there is already something like this answered. I don't quite understand the examples given there.
function joinBestelling (){
$sql =( "SELECT
artikel.artikelCode,
factuurregel.aantal
FROM artikel
INNER JOIN factuurregel
ON artikel.artikelCode = factuurregel.artikelCode");
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$records = $stmt->fetchAll();
return $records;
}
so i have two tables now artikel and factuurregel how do i add like 3 more tables with info in the join ?
Simply join the outher tables like the first one:
SELECT
artikel.artikelCode,
factuurregel.aantal
FROM artikel
INNER JOIN factuurregel
ON artikel.artikelCode = factuurregel.artikelCode
INNER JOIN table3 on .....
INNER JOIN table4 on .....
Hopefully that was your question
function joinBestelling (){
$sql =( "SELECT factuurregel.factuurNummer, factuurregel.artikelCode, factuurregel.aantal, factuurregel.prijs, factuur.factuurNummer, factuur.factuurDatum, factuur.klantCode, klant.klantCode, klant.voorLetter, klant.achterNaam, artikel.artikelCode, artikel.artikel, artikel.prijs FROM factuurregel JOIN factuur ON factuurregel.factuurNummer = factuur.factuurNummer JOIN klant ON factuur.klantCode = klant.klantCode JOIN artikel ON factuurregel.artikelCode = artikel.artikelCode");
$stmt = $this->conn->prepare($sql);
$stmt->execute();
$records = $stmt->fetchAll();
return $records;
}

Mysql php query doesn't work with pdo

I have a question.
This is my query:
DROP VIEW IF EXISTS Bedrijven_speci;
CREATE VIEW Bedrijven_speci as SELECT bedrijfgegevens.id,
bedrijfgegevens.subbranche_id ,
(6371 * acos(cos(radians(51.61162253395352)) * cos(radians(bedrijfgegevens.latitude)) * cos(radians(bedrijfgegevens.longitude) - radians(5.535746802487663)) + sin (radians(51.61162253395352)) * sin (radians(bedrijfgegevens.latitude))) ) AS distance
FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE branches.naam = "Fotografie" AND subbranches.naam = "fotografen"
GROUP BY bedrijfgegevens.id HAVING distance < 10;
SELECT branches.naam as branche_naam, subbranches.naam as subbranche_naam, specialiteiten.naam as specialiteiten_naam
FROM Bedrijven_speci
INNER JOIN subbranches on subbranches.id = Bedrijven_speci.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = Bedrijven_speci.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
$query;
$sth = $pdo->prepare($query);
$sth->execute();
$fetch = $sth->fetchAll();
It works when I execute the query in Phpmyadmin. But when I execute in with PDO it returns an empty array. Can someone help me?
Just execute the statements one after another.
If required start a transaction before executing the the statements via PDO::beginTransaction
<?php
$queries = array(
'DROP VIEW IF EXISTS Bedrijven_speci',
'CREATE VIEW Bedrijven_speci as
SELECT
bedrijfgegevens.id,bedrijfgegevens.subbranche_id,
(6371 * acos(cos(radians(51.61162253395352)) * cos(radians(bedrijfgegevens.latitude)) * cos(radians(bedrijfgegevens.longitude) - radians(5.535746802487663)) + sin (radians(51.61162253395352)) * sin (radians(bedrijfgegevens.latitude))) ) AS distance
FROM bedrijfgegevens
INNER JOIN subbranches on subbranches.id = bedrijfgegevens.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = bedrijfgegevens.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id
WHERE branches.naam = "Fotografie" AND subbranches.naam = "fotografen"
GROUP BY bedrijfgegevens.id HAVING distance < 10',
'SELECT branches.naam as branche_naam, subbranches.naam as subbranche_naam, specialiteiten.naam as specialiteiten_naam
FROM Bedrijven_speci
INNER JOIN subbranches on subbranches.id = Bedrijven_speci.subbranche_id
INNER JOIN branches on branches.id = subbranches.branche_id
INNER JOIN bedrijfgegevens_specialiteiten on bedrijfgegevens_specialiteiten.bedrijfgegevens_id = Bedrijven_speci.id
INNER JOIN specialiteiten on specialiteiten.id = bedrijfgegevens_specialiteiten.specialiteiten_id'
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
try {
foreach( $queries as $q ) {
$pdo->exec($q);
}
}
catch(Exception $ex) {
$pdo->rollBack();
throw $ex;
}
$pdo->commit();

Doctrine 2 ResultSetMapping Many-To-Many example

I'm trying to use ResultSetMappingBuilder to get data from Native query.
$sql = "SELECT e.start_date FROM se_events e
LEFT JOIN se_event_tags tg ON e.id = tg.event_id
LEFT JOIN se_event_type t ON tg.event_type_id = t.id
WHERE t.id = :id";
I have no idea how to build ResultSetMappingBuilder.
public function createResultSetMapping() {
$rsm = new \Doctrine\ORM\Query\ResultSetMappingBuilder($this->getEntityManager());
$rsm->addRootEntityFromClassMetadata('Event', 'e');
return $rsm;
}
Thanks for help in advance.
So I've used row SQL:
$sql = "SELECT e.start_date FROM se_events e
LEFT JOIN se_event_tags tg ON e.id = tg.event_id
LEFT JOIN se_event_type t ON tg.event_type_id = t.id
WHERE t.id = $id";
$stmt = $this->getEntityManager()->getConnection()->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();

Categories