简体   繁体   English

使用带有多个内部连接的 PDO 查询获得正确的结果。 前面没有读到结果?

[英]Get a correct result with a PDO query with several inner join. Result not read in front?

I am blocked since 2 days about this problem.因为这个问题我被封锁了 2 天。

I want to display an info from other table in my "info_array".我想在我的“info_array”中显示来自其他表的信息。

http://www.noelshack.com/2022-32-4-1660224518-type-miss.png http://www.noelshack.com/2022-32-4-1660224518-type-miss.png

Td's "Type d'alarme" has the missing info, which calls another table. Td 的“Type d'alarme”缺少信息,它调用了另一个表。 But i can't explain why.但我无法解释为什么。 All info for client call the table client, "Type d'alarme" calls type_alarme's table.客户端的所有信息都调用表客户端,“Type d'alarme”调用 type_alarme 的表。 There is no column connexion between them.它们之间没有列连接。 That's why I use "inner join" in my PDO query.这就是我在 PDO 查询中使用“内连接”的原因。

I searched different method to join the result of the 2 tables but no success.我搜索了不同的方法来加入 2 个表的结果,但没有成功。

Here my HTML code for this part (it concerns "td id="alarme_type_client"></td"):这是我的 HTML 代码(它涉及“td id="alarme_type_client"></td”):

 <table id="info_client" border=1>
    <thead>
      <tr>
        <th>#</th>
        <th>Nom</th>
        <th>Prénom</th>
        <th>Date de naissance</th>
        <th>Adresse</th>
        <th>Adresse mail</th>
        <th>Téléphone</th>
        <th>Age</th>
        <th>Type d'alarme</th>

      </tr>
    </thead>

    <tbody>
      <tr id=<?php echo $client["id_client"]; ?>>
        <td id="id"></td>
        <td id="nom"></td>
        <td id="prenom"></td>
        <td id="date" name="date"></td>
        <td id="adresse"></td>
        <td id="mail"></td>
        <td id="tph"></td>
        <td id="age"></td>
        <td id="alarme_type_client"></td>
        <td><button data-id="<?php echo $client["id_client"]; ?>" type="button" class="hide_client">Masquer client</button></td>
        </td>
      </tr>
    </tbody>
  </table>

My ajax function:我的 ajax function:

$(document).ready(function () {
  $(".info").click(function () {
    var datas = {
      cmd: 'id_client',
      id_client: $(this).attr('data-id'),
    };
    $.ajax({
      type: "GET",
      url: "function.php",
      data: datas,
      cache: false,
    }).done(function (result) {
      $('#id').html(result.id_client)
      $('#nom').html(result.nom_client),
        $('#prenom').html(result.prenom_client),
        $('#date').html(result.client_date_naissance),
        $('#adresse').html(result.client_adresse),
        $('#mail').html(result.client_mail),
        $('#tph').html(result.client_tph),
        $('#age').html(result.age),
        $('#alarme_type_client').html(result.nom_type_alarme),
        console.log(result.id_client),
      $("#info_client").css("display", "block");

My PHP function:我的 PHP function:

function read()
{
  global $db;
  $id_client = $_GET['id_client'];
  $sql ="SELECT client.id_client,client.nom_client,client.prenom_client,client.client_date_naissance,client.client_adresse,client.client_mail,client.client_tph, type_alarme.nom_type_alarme, YEAR(CURDATE( )) - YEAR(client_date_naissance) - CASE WHEN MONTH(CURDATE( )) < MONTH(client_date_naissance) OR (MONTH(CURDATE( )) = 
  MONTH(client_date_naissance) AND DAY(CURDATE( )) < DAY(client_date_naissance)) THEN 1 ELSE 0 END AS age FROM client; 
  INNER JOIN alarme ON alarme.id_client=client.id_client;
  INNER JOIN type_alarme on type_alarme.id_type_alarme = alarme.id_type_alarme
  WHERE id_client = :id_client";
  $query = $db->prepare($sql);
  $query->bindValue(':id_client', $id_client, PDO::PARAM_INT);
  $query->execute();
  $result = $query->fetch(PDO::FETCH_ASSOC);
  return ($result);
}

http://www.noelshack.com/2022-32-4-1660225072-resultat-sql.png http://www.noelshack.com/2022-32-4-1660225072-resultat-sql.png

In Heidi SQL (Laragon), the query works.在 Heidi SQL (Laragon) 中,查询有效。 I use the 2 inner join 'cause alarme is my principal table which union the info what I need我使用 2 内连接,因为警报是我的主表,它结合了我需要的信息

It displays the info client if I erase the " type_alarme.nom_type_alarme" at the beginning of query.如果我在查询开始时删除“type_alarme.nom_type_alarme”,它会显示信息客户端。

Any ideas?有任何想法吗? I don't find solution.我没有找到解决方案。

Thanks for help.感谢帮助。

It is because you are not joining the type_alarme table at all.这是因为您根本没有加入type_alarme表。 Why?为什么? Because in your SQL query you have a semicolon ;因为在您的 SQL 查询中,您有一个分号; just before you do INNER JOIN type_alarme .就在你做INNER JOIN type_alarme之前。 So the query is interrupted there and hence the type_alarme.nom_type_alarme in the SELECT clause doesn't know which table and column is that.因此查询在那里被中断,因此type_alarme.nom_type_alarme子句中的SELECT不知道是哪个表和列。

Remove the semicolon and it will work.删除分号,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM