dsa45664 2017-09-25 18:58
浏览 133
已采纳

如何在查询中关联两个表?

I saved a value as a cookie and then checked if there exists in the perfiles_vinculados table to get all the data that has the same id in the perfil table.

Then I create an array of the $vinculado result and show it in a HTML table as a row.

The problem is that the console returns:

Catchable fatal error:
Object of class mysqli_result could not be converted to string in C:\xampp\htdocs\miramonteapp\api\modal.php

The cookie:

document.cookie = "vinculaciones=" + $("#mod_id_perfil").val();

PHP:

//querys

<?php

include 'api/conexion.php';
$ides = $_COOKIE['vinculaciones'];
$juridicos = "SELECT perfil_juridica FROM perfiles_vinculados where  perfil_fisica = '$ides'";
$con = mysqli_query($conexion, $juridicos);
$vinculado = mysqli_query($conexion, "SELECT * FROM perfil where  id = '$con'");

?>

//table

<?php 
while($reg = mysqli_fetch_array($vinculado)) {
    $id = $reg['id'];  
?>
<tr id="<?php echo " tr_ ".$reg['id']; ?>">
  <td class="" data-id="<?php echo $reg['usuario'] ?>">
    <?php echo $reg['nombre']; ?>
  </td>
  <td class="" data-id="<?php echo $reg['usuario'] ?>">
    <?php echo $reg['cuit']; ?>
  </td>
  <td class="td-actions text-right">
    <button type="button" rel="tooltip" class="btn btn-danger">
      <i class="material-icons">close</i>
    </button>
  </td>
<?php } ?>
  • 写回答

1条回答 默认 最新

  • duanjuebiao6730 2017-09-25 19:06
    关注

    You have to learn about join sql statement.

    As for you current approach, first you need to fetch perfil_juridica value from a result of $juridicos execution and then pass this value to your second query:

    // first query
    $juridicos = "SELECT perfil_juridica FROM perfiles_vinculados where  perfil_fisica = '$ides'";
    $result = mysqli_query($conexion, $juridicos);
    $row = mysqli_fetch_array($result);
    $perfil_juridica = $row['perfil_juridica'];
    // second query
    $vinculado = mysqli_query($conexion, "SELECT * FROM perfil where  id = '$perfil_juridica'");
    

    What you should do next is move to prepared statements instead of putting unsafe values into query texts. This question will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Thoony写ESP32温湿度检测无源蜂鸣器报警代码?
  • ¥20 部件内部的CT图像数据集
  • ¥15 Visual studio调用动态库
  • ¥15 双目摄像头标定后的校准文件
  • ¥15 powerbi举证增加度量值后出现对应关系错乱
  • ¥30 频率分析法分析绘制奈奎斯特图、波特图
  • ¥15 弹出来一万个系统找不到指定的文件框框,怎么解决
  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow