使用mysql fetch_object从多个表中选择数据

时间:2017-06-15 04:58:56

标签: php mysql html5

我是网络应用程序和使用关系数据库的新手,我正在尝试使用我在一个表格中同时从5个表中创建selectprint YouTube视频,在第一个select我正在选择一个人并将他的所有数据存储在变量中,包括外键,之后我使用存储在人的变量中的外键从其他4个表中选择数据最后我打算在表格行上打印所有数据。

我知道这个方法很实用我已经用它来完美地从单个表中进行选择但是这里尝试选择表单多个表不起作用,也许我应该尝试在第一个选择中使用JOIN并选择所有从那里的桌子。在我运行这个php脚本的地方,它会返回以下错误:

  

未定义的变量:第71行的C:\ wamp64 \ www \ sicu \ paciente.php中的db“和这个”致命错误:在C:\ wamp64 \ www \ sicu \中调用null上的成员函数查询()第71行的paciente.php

    <?php
    $sql = "SELECT * from `paciente` ORDER BY `ci_p`";
    $result = $db-> query ($sql);

    while ($row = $result ->fetch_object()) {



    $ci_p = $row -> ci_p;
    $primer_nombre =htmlentities ($row -> primer_nombre, ENT_QUOTES, "UTF-8");
    $segundo_nombre = htmlentities ($row -> segundo_nombre, ENT_QUOTES, "UTF-8");
    $primer_apellido = htmlentities ($row ->primer_apellido , ENT_QUOTES, "UTF-8"); 
    $segundo_apellido = htmlentities ( $row -> segundo_apellido, ENT_QUOTES, "UTF-8");
    $sexo = htmlentities ( $row -> sexo, ENT_QUOTES, "UTF-8");
    $tipo_p = htmlentities ( $row -> tipo_p, ENT_QUOTES, "UTF-8");
    $tipo_personal = htmlentities ( $row -> tipo_personal, ENT_QUOTES, "UTF-8");
    $especialidad= htmlentities ( $row -> especialidad, ENT_QUOTES, "UTF-8");
    $alergias = htmlentities ( $row -> alergias, ENT_QUOTES, "UTF-8");
    $correo_id_correo = htmlentities ( $row -> correo_id_correo, ENT_QUOTES, "UTF-8");
    $telefono_id_telefono = htmlentities ( $row -> telefono_id_telefono, ENT_QUOTES, "UTF-8");
    $direccion_id_direccion = htmlentities ($row -> direccion_id_direccion , ENT_QUOTES, "UTF-8");
    $fecha_id_fecha = htmlentities ($row -> fecha_id_fecha , ENT_QUOTES, "UTF-8");

    $sql1 = "SELECT * from `direccion` where id_direccion = '$direccion_id_direccion')";
    $result1 = $db-> query ($sql1);
    while ($row1 = $result1 ->fetch_object()){
        $estado =htmlentities ($row -> estado, ENT_QUOTES, "UTF-8");
        $ciudad = htmlentities ($row -> ciudad, ENT_QUOTES, "UTF-8");
        $municipio = htmlentities ($row ->municipio , ENT_QUOTES, "UTF-8"); 
        $parroquia = htmlentities ( $row -> parroquia, ENT_QUOTES, "UTF-8");
        $sector = htmlentities ( $row -> sector, ENT_QUOTES, "UTF-8");
        $edificio = htmlentities ( $row -> edificio, ENT_QUOTES, "UTF-8");
        $piso= htmlentities ( $row -> piso, ENT_QUOTES, "UTF-8");
        $numero = $row -> numero;
        $punto_ref = htmlentities ( $row -> punto_ref, ENT_QUOTES, "UTF-8");
        $avenida = htmlentities ( $row -> fecha_ingre, ENT_QUOTES, "UTF-8");

    }
    $sql2 = "SELECT * from `correo_electronico` where id_correo = '$correo_id_correo')";
    $result2 = $db-> query ($sql2);
    while ($row2 = $result2 ->fetch_object()){
        $direccion_correo =htmlentities ($row -> direccion_correo, ENT_QUOTES, "UTF-8");

    }
        $sq3 = "SELECT * from `telefono` where id_telefono = '$telefono_id_telefono')";
    $result3 = $db-> query ($sql3);
    while ($row3 = $result3 ->fetch_object()){
    $numero_telefono = $row -> numero_telefono;

    }
        $sql5 = "SELECT * from `fecja_naci` where id_fecha_naci = '$fecha_id_fecha')";
    $result5 = $db-> query ($sql5);
    while ($row5 = $result5 ->fetch_object()){
    $fecha =htmlentities ($row -> fecha, ENT_QUOTES, "UTF-8");

    }
?>
    <tbody><tr>
        <td><?php echo $ci_p?></td>
        <td><?php echo " $primer_nombre $segundo_nombre $primer_apellido $segundo_apellido "?></td>
        <td><?php echo $sexo ?></td>
        <td><?php echo $tipo_p ?></td>
        <td><?php echo $tipo_personal?></td>
        <td><?php echo $especialidad?></td>
        <td><?php echo $alergias ?></td>
        <td><?php echo $fecha?></td>
        <td><?php echo $numero_telefono?></td>
        <td><?php echo $direccion_correo?></td>
        <td><?php echo "$estado $ciudad $municipio $parroquia $sector $edificio $piso $numero $punto_ref $avenida"?></td>
    </tr></tbody>
<?php
}
?>

    

1 个答案:

答案 0 :(得分:0)

首先,创建一个文件connection.php

@Component({
  selector: 'copy',
  template: '...<ng-content></ng-content>...'
})
class CopyComponent {
  @ContentChild('v') projected;

  ngAfterViewInit() {
     console.log(this.projected.nativeElement.innerText); // 'some value'
  }
}

@Component({
   selector: 'parent',
   templates: '...<copy><span #v>{{prop}}</span></copy>...'
})
class Parent {
  prop = 'some value';
}

然后添加此代码:

<?php

$db = new mysqli("Host name","User","DB Password","DB Name");

// ceck connection

if ($db->connect_error) {
   die('Connection failed: '. $connect->connect_error);
}
?>

在你的php文件中。

我的建议使用mysql中的内连接