2011-06-03 179 views
0

我有兩個表,我想從第一個表中拉ID(如果它不存在插入,然後拉ID),並使用ID在第二個表中查找另一個值(如果沒有找到插入)。但由於缺乏對mysql查詢如何工作的理解,我無法找到如何... 當前查詢看起來像;我認爲第一部分正在工作(尋找現有條目並插入,如果它不存在),但由於某些原因,我無法連接到我的代碼的「路徑」部分。存儲MySQL查詢作爲另一個查詢的變量

請提供一些線索......

$sqlcheckforexisting = "SELECT * 
          FROM firsttable 
         WHERE firsttable.data = 'DATA' "; 
$sqlselect = "SELECT firsttable.ID 
       FROM firsttable 
       WHERE firsttable.data = 'DATA'"; 
$sqlinsert = "INSERT INTO firsttable 
       (data) 
       VALUES 
       ('DATA')"; 

if(mysqli_num_rows(mysqli_query($link,$sqlcheckforexisting)) == 1) { 
    $ID = mysqli_query($link,$sqlselect); 

    if(!$ID) { 
    echo 'error selecting the id'. mysqli_error($link); 
    include 'error.html.php'; 
    exit(); 
    } 
} 

if(mysqli_num_rows(mysqli_query($link,$sqlcheckforexisting)) == 0) { 
    mysqli_query($link,$sqlinsert); 
    $ID = mysqli_query($link,$sqlselect); 

    if(!$ID) { 
    echo 'error selecting the n id'. mysqli_error($link); 
    include 'error.html.php'; 
    exit(); 
    } 
} 

$sqlcheckpath = "SELECT * 
        FROM path 
        WHERE path.id = $ID 
        AND path.path = 'path' "; 
$sqlselectpath = "SELECT firsttable.ID 
        FROM path 
        WHERE firsttable.data = 'DATA'"; 
$sqlinsertpath = "INSERT INTO path 
        (firsttableID, path) 
        VALUES 
        ('$ID', 'path')"; 

if(mysqli_num_rows(mysqli_query($link, $sqlcheckpath)) == 1) { 
    $pathID = mysqli_query($link, $sqlselectpath); 

    if(!$pathID) { 
    echo 'error selecting the id'. mysqli_error($link); 
    include 'error.html.php'; 
    exit(); 
    } 
} 

if(mysqli_num_rows(mysqli_query($link, $sqlcheckpath)) == 0) { 
    mysqli_query($link,$sqlinsertpath); 
    $pathID = mysqli_query($link, $sqlselectpath); 

    if(!$pathID) { 
    echo 'error selecting the n id'. mysqli_error($link); 
    include 'error.html.php'; 
    exit(); 
    } 
} 
+0

中有語法錯誤上面的代碼,所以它不會工作,首先解決這些問題。 – 2011-06-03 03:49:25

回答

0

希望這將有助於。我沒有對它進行測試,因此我的標準免責聲明可能需要進行一些調整。如果你解決了一些我已經搞砸了的東西,讓我知道在評論中,我會編輯答案以反映它,以防有人通過稍後通過搜索發現此問題。

$db = mysqli_connect($host, $user, $password, $database); 
$sql = "SELECT id FROM firsttable WHERE data = 'DATA';"; 
$result = mysqli_query($db, $sql); 
if (($row = mysqli_fetch_assoc($result)) !== NULL) { 
    // The row existed in firsttable. 
    $id = $row['id']; 
} 
else { 
    $sql = "INSERT INTO firsttable (data) VALUES ('DATA');"; 
    mysqli_query($db, $sql); 
    $id = mysqli_insert_id($db); 
} 

// Okay, now we have the id of the row in firsttable. We can use it to perform 
// operations in the path table. 

老實說,我不確定你想用路徑表做什麼。它看起來像你試圖拉第一張表(firsttable.ID,firsttable.data),這是你不能沒有一個JOIN。如果你只是希望找到第二個表有一個相應的ID作爲第一個表中的字段,你可以使用:

$sql = "SELECT id, path /* , other fields... */ FROM path WHERE id = ?;"; 
$query = mysqli_stmt_init($db); 
if (mysqli_prepare($query, $sql)) { 
    mysqli_stmt_bind_param($query, 'i', $id); // 's' if $id is a string 
    mysqli_stmt_execute($query); 

    if ($result = mysqli_stmt_get_result($query)) { 
     if (($row = mysqli_fetch_assoc($result)) !== NULL) { 
      // $row now contains the fields from the path table that 
      // corresponds to the $id fetched from firsttable. 
     } 
     else { 
      // There is no row in the path table that corresponds to the $id 
      // from firsttable. 
     } 
    } 

} 

希望這有助於--KS

+0

感謝KS,我知道我沒有解釋清楚,但第二個查詢正是我想要做的。儘管如此,我確實有一個問題。在SQL查詢中,您使用了「WHERE id =?」爲什麼我不能使用「id = $ id」? – 2011-06-03 17:40:42

+0

你可以,但如果你這樣做,確保$ id是消毒。如果這是從POST,GET或COOKIE讀取的$ id,則需要確保它不包含任何惡意或非法輸入。您可以像上面那樣使用參數化查詢,也可以使用preg_match()語句來確保用戶不會嘗試執行SQL注入攻擊。 – 2011-06-03 17:44:08

+0

只是運行查詢,並得到錯誤「警告:mysqli_stmt_bind_param()期望參數1爲mysqli_stmt,布爾給定」的行包括mysqli_stmt_bind_param,mysqli_stmt_execute。致命錯誤:調用mysqli_stmt_get_result的未定義函數mysqli_stmt_get_result()。 – 2011-06-03 17:59:03

相關問題