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 刚接触VFP,我想知道为什么是这种情况,有人可以看看吗
  • ¥15 io.jsonwebtoken.security.Keys
  • ¥15 急,ubuntu安装后no caching mode page found等
  • ¥15 联想交换机NE2580O/NE1064TO安装SONIC
  • ¥15 防火墙的混合模式配置
  • ¥15 Ubuntu不小心注销了要怎么恢复啊
  • ¥15 win10电脑安装完plcsim advanced4.0运行时为什么会提示找不到虚拟网卡
  • ¥15 安装powerbuilder10卡在安装程序正在运行这个页面 没有下一步任何指令
  • ¥15 关于mpi的问题:请问遇到这种情况需要怎么解决,出现这个问题后电脑不能进行mpi多核运行只能进行单核运行
  • ¥50 微信聊天记录备份到电脑提示成功了,但还是没同步到电脑微信