2012-09-07 108 views
0

搜索多個表我以前問一個問題MYSQL Searching multiple tables with different columns using LIKEMYSQL按照特定的順序

這點我是無法得到的幫助:所以我妥協。 現在,如果一個人在多個表中搜索並找到它,它不會按照我想要的順序搜索它們。我希望結果首先來自連接器表,然後是適配器表,然後是組件表。如果在連接器中找到結果,請不要搜索適配器或組件,只需繼續。

我也試過把它們放在這個順序中,而不使用其他的如果:組件,適配器,連接器。但這並不奏效。

if(isset($_GET['num'])) { 
$num = $_GET['num']; 
$numresult = mysql_query("SELECT * FROM productnumber WHERE part_num LIKE '%$num%'"); 

    if ($numresult) { 

    while ($row = mysql_fetch_array($numresult)) { 

     if ($row["title"] == "connectors") { 
      $numtitle = "connectors"; 
      $result = mysql_query("SELECT * FROM connectors WHERE part_num LIKE '%$num%'"); 
     } else if ($row["title"] == "adapters") { 
      $numtitle = "adapters"; 
      $result = mysql_query("SELECT * FROM adapters WHERE part_num LIKE '%$num%'"); 
     } else if ($row["title"] == "components") { 
      $numtitle = "components"; 
      $result = mysql_query("SELECT * FROM components WHERE part_num LIKE '%$num%'"); 
     } 

    } 
    } 
} 
+0

您已開放SQL注入。不要使用'mysql_ *'方法。他們在您的網站上造成一個很大的安全漏洞。使用PDO代替.. –

+0

嘿謝謝你的提示,不知道這個 – Jweb

回答

1

這是使用mysql_*方法的代碼:此代碼將在連接器搜索如果沒有找到那麼它會在搜索適配器等。但是按照您的要求如果它在連接器中找到了某些東西,它將不會檢入適配器或組件!

<?php 
if(isset($_GET['num'])) { 
$num = $_GET['num']; 
    // We will first search for connectors 
    $result = mysql_query("SELECT * FROM connectors WHERE part_num LIKE %'$num'%"); 
    if(mysql_num_rows($result) == 0) { 
    // Now if we don't find anything in connectors, we will search in adapters 
    $result = mysql_query("SELECT * FROM adapters WHERE part_num LIKE %'$num%'%"); 
    if(mysql_num_rows($result) == 0){ 
    // And if we don't find anything in adapters, finally we will search in components 
    $result = mysql_query("SELECT * FROM components WHERE part_num LIKE %'$num%'%"); 
    if(mysql_num_rows($result) == 0) { 
     echo "We Found Nothing! :(" 
    } 
    }} 
} 
?> 

這是一個示例代碼(只是您的代碼)轉換爲PDO。但對於你的問題不是正確的答案。

<?php 
    try { 
     $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
     $qry = $conn->prepare('SELECT * FROM productnumber WHERE part_num LIKE %:part_num%'); 
     $qry->execute(array(':part_num'=>$_GET['num'])); 
     $row = $qry->fetch(PDO::FETCH_OBJ) 
      switch ($row->title) { 
       case 'connectors': 
       $numtitle="connectors"; 
       $qry2=$conn->prepare('SELECT * FROM connectors WHERE part_num LIKE %:num%'); 
       $qry2->execute(array(':num'=>$_GET['num']))); 
       break; 
       case 'adapters': 
       $numtitle="adapters"; 
       $qry2=$conn->prepare('SELECT * FROM adapters WHERE part_num LIKE %:num%'); 
       $qry2->execute(array(':num'=>$_GET['num']))); 
       break; 
       case 'components': 
       $numtitle="components"; 
       $qry2=$conn->prepare('SELECT * FROM components WHERE part_num LIKE %:num%'); 
       $qry2->execute(array(':num'=>$_GET['num']))); 
       break; 
      } 
    } catch(PDOException $e) { 
     echo 'ERROR: ' . $e->getMessage(); 
    } 
    ?> 
+0

感謝給我一個PDO的例子。你能用我現在的代碼給我一個簡單的例子嗎?即使它可能不安全。我很樂意給你接受的答案 – Jweb

+0

@Jweb我已經用代碼更新了答案! –