我真的是新的網絡應用程序和使用關係數據庫,我試圖從5個表中同時使用這種方法,我看到在一個select
和print
YouTube視頻,在第一個select
我選擇一個人並將他的所有數據存儲在包括外鍵的變量中,然後我使用存儲在變量中的外鍵從其他4個表中選擇數據,最後我打印表格行上的所有數據。使用mysql從多個表中選擇數據fetch_object
我知道該方法是功能我已經用它從一張表中完美地選擇,但在這裏試圖選擇形式多個表不起作用,也許我應該嘗試在第一個選擇和selecto從所有表中的JOIN
。當我運行這個PHP腳本返回該錯誤:
Undefined variable: db in C:\wamp64\www\sicu\paciente.php on line 71" and this " Fatal error: Call to a member function query() on null in C:\wamp64\www\sicu\paciente.php on line 71
<?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
}
?>
你需要一個工作的mysqli連接,您可以查詢之前,像'$ DB =新的mysqli( 'localhost' 的, 'my_user', 'MY_PASSWORD', 'MY_DB' );' – Rasclatt
我有一個conect.php文件,忘記包含它,然後將該方法更改爲內部聯接_italic _ ** bold **'include「php/connect.php」; $ SQL = 「SELECT * FROM paciente INNER JOIN direccion ON direccion_id_direccion = id_direccion 內部聯接telefono上telefono_id_telefono = id_telefono INNER JOIN上correo_id_correo correo_electronico = id_correo INNER JOIN上fecha_id_fecha = id_fecha_naci ORDER fecha_naci BY'ci_p'」; $ result = $ db-> query($ sql)' –