如何从两个表中获取数据PHP

时间:2018-10-01 20:54:17

标签: php mysql

您好,我要执行此操作,我有一个名为Users的表,有可以登录的用户,也有Profile的表。因此,我如何将其合并到一个php sript中,例如,我正在使用用户的数据登录到页面,并向其显示了Profile的详细信息。请帮助我,抱歉我的英语不好,希望您能理解我。

 <html>
<body>
<form method="POST">
<input type="text" name="username" placeholder="Username">
<br/>
<input type="password" name="password" value="Password">

<input type="submit" name="submit" value="Login">
</form>
</body>
</html>

<?php

require "connection.php";

$username=$_POST ['username'];
$password=$_POST ['password'];

$sqlselect="SELECT * FROM `Users` WHERE `username`='".$username."' AND `password`='".$password."';";
$result=mysqli_query ($con,$sqlselect);

$response=array();

echo "<table border='3'>";
    echo "<tr>";
        echo "<th> Username</th>";
        echo "<th> Password </th>";
        echo "<th> Email </th>";
echo "</tr>";

while ($row=mysqli_fetch_array ($result))
{
    $response=array ("username"=>$row[0],"password"=>$row[1],"email"=>$row[2]);
       echo "<tr>";
           echo "<td>". $row ['username'] ."</td>";
           echo "<td>". $row ['password'] ."</td>";
           echo "<td>". $row ['email'] ."</td>";
       echo "</tr>";
}
echo "</table>";



$sqlselect="SELECT * FROM `Profile` WHERE `location`='".$location."' AND `ssid`='".$ssid."';";
$result=mysqli_query ($con,$sqlselect);

$rows=array();

echo "<table border='3'>";
    echo "<tr>";
        echo "<th> Username</th>";
        echo "<th> Password </th>";
        echo "<th> Email </th>";
echo "</tr>";

while ($row=mysqli_fetch_array ($result))
{
    $rows=array ("location"=>$row[0],"test"=>$row[1],"country"=>$row[2]);
       echo "<tr>";
           echo "<td>". $row ['username'] ."</td>";
           echo "<td>". $row ['password'] ."</td>";
           echo "<td>". $row ['email'] ."</td>";
       echo "</tr>";
}
echo "</table>";




  echo json_encode($rows,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_LINE_TERMINATORS);

?>

1 个答案:

答案 0 :(得分:-2)

喜欢吗? (不要忘记更改名称)

$sqlselect="
SELECT 
usu.id_user_table_here, 
usu.username, 
usu.email,
prof.ssid,
prof.location
FROM Users AS usu
INNER JOIN profile AS prof ON prof.ssid=usu.id_user_table_here
WHERE 
username = '".$username."' 
AND 
password = '".$password."'";