开发者

storing mysql query as a variable for another query

I have two tables where I want to pull ID (if it doesn't exist insert, then pull id) from first table and use the ID to look up another value in second table (if does not find insert). But due to lack of my understanding of how mysql query works, I cannot find out how... The Current query looks like; I think the first part is working (looking for existing entry and inserting if it does not exist), but for somereason I cannot bridge to the "path" portion of my code.

Please shed some light...

$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.da开发者_StackOverflow社区ta = '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();
  }
}


I haven't tested it, so my standard disclaimer that some tweaking may be necessary applies.

$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.

Honestly, I'm not sure what you're trying to do with the path table. It looks like you're trying to pull fields from the first table (firsttable.ID, firsttable.data), which you can't do without a JOIN. If you're just looking to find the fields in the second table that have a corresponding id as the first table, you could use:

$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.
        }
    }
    
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜