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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分