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

时间:2017-09-25 18:58:39

标签: javascript php html mysql

我将值保存为cookie,然后检查perfiles_vinculados表中是否存在以获取perfil表中具有相同ID的所有数据。

然后我创建一个$vinculado结果的数组,并将其作为一行显示在HTML表格中。

问题是控制台返回:

  

可捕获的致命错误
  mysqli_result类的对象无法转换为字符串    C:\ XAMPP \ htdocs中\ miramonteapp \ API \ modal.php

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 个答案:

答案 0 :(得分:1)

您必须了解join sql语句。

至于您当前的方法,首先需要从perfil_juridica执行结果中获取$juridicos值,然后将此值传递给第二个查询:

// 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'");

接下来应该做的是转到准备好的语句,而不是将不安全的值放入查询文本中。 This question会帮助你。