PHP多个数据库表搜索

时间:2014-08-14 08:15:07

标签: php mysql sql union

我正在尝试使用UNION在多个表中搜索相同的关键字。

我的代码如下:

$con = new PDO( DB_HOST, DB_USER, DB_PASS ); 
    $con->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    $sql = "SELECT * FROM evidence_vin WHERE vin = :vin LIMIT 1";
    $sql2 = "SELECT * FROM national_register_sk WHERE vin = :vin LIMIT 1";

    $stmt = $con->prepare( $sql ) union ( $sql2 );
    $stmt->bindValue( "vin", $this->vin, PDO::PARAM_STR );
    $stmt->execute();
        echo "<table>";
        echo "<th>Progress</th>";
        echo "<th>Claim number</th>";
        echo "<th>Make</th>";
        echo "<th>Status</th>";
        echo "<th>View</th>";
        echo "<th>Action</th>";
        while ($row = $stmt->fetch()){
            echo "<tr>";
            echo "<td>24</td>";
            echo "<td>".$row['claim_number']."</td>";
            echo "<td>".$row['license']."</td>";
            echo "<td>".$row['country']."</td>";
            echo "<td>".$row['vin']."</td>";
            echo "<td><a href=\"detail.php?id=".$row["id"]."&action=detail\">detail</td>";
            echo "</tr>";
             } 
        }catch(PDOExeption $e){
        echo $e->getMessage();

Ofc它与意想不到的联盟的投掷错误,但我不知道我应该怎么写它。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:2)

您的联盟必须在SQL语句中。

$sql = "SELECT * FROM evidence_vin WHERE vin = :vin LIMIT 1 UNION SELECT * FROM national_register_sk WHERE vin = :vin LIMIT 1";
相关问题