我有兩個表,我想從第一個表中拉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();
}
}
中有語法錯誤上面的代碼,所以它不會工作,首先解決這些問題。 – 2011-06-03 03:49:25