sql与搜索一样,如何根据搜索结果从一个表中提取数据

时间:2014-04-29 19:54:43

标签: php mysql sql

我有两张桌子:

table1 name user             DATA( ID, AGE, NAME,PLACE)
table2 name personalinfo     DATA( ID, EMAIL, COUNTRY, JOB)

并且每个用户都有UNIQUE ID个表。

如果我在第一张桌子上搜索并得到一些结果,我如何使用第一次搜索得到的ID结果从第二张表中提取数据?

这是我的代码:

<?php
    include_once("php_files/db_conx.php");
    $sql = "(SELECT id, age FROM user WHERE age LIKE '$searchquery')";
    $user_query = mysqli_query($db_conx, $sql);
    $count = mysqli_num_rows($user_query);
    if($count > 0){
        $search_output .= "<hr />$count results for $searchquery <hr/>";
        while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)){
            $id = $row["id"];
            $age = $row["age"];
            $search_output .= "Item ID: $id - $age<br />";
        } // close while
    } else {
        $search_output = "<hr />0 results for $searchquery<hr/>";
    }
?>

1 个答案:

答案 0 :(得分:0)

在您的代码

之后尝试此操作
foreach($id as $id_table2){
$sql = "(SELECT id, email, country, job FROM personalinfo WHERE id =' $id_table2')";
$user_query = mysqli_query($db_conx, $sql);
$count = mysqli_num_rows($user_query);
   if($count > 0){
          while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC))
             print_r($row);
   }
}