将mysql查询存储为另一个查询的变量

时间:2011-06-03 03:45:02

标签: php mysql

我有两个表,我想从第一个表中提取ID(如果它不存在insert,然后是pull id)并使用ID在第二个表中查找另一个值(如果找不到insert)。但是由于我对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();
  }
}

1 个答案:

答案 0 :(得分: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