Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将mysql查询存储为另一个查询的变量_Php_Mysql - Fatal编程技术网

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

Php 将mysql查询存储为另一个查询的变量,php,mysql,Php,Mysql,我有两个表,我想从第一个表中提取ID(如果不存在insert,则提取ID),并使用该ID在第二个表中查找另一个值(如果找不到insert)。但由于我对mysql查询的工作原理缺乏了解,我无法了解如何。。。 当前查询如下所示:;我认为第一部分是有效的(查找现有条目,如果不存在则插入),但由于某种原因,我无法连接到代码的“路径”部分 请给我点光 $sqlcheckforexisting = "SELECT * FROM firsttable

我有两个表,我想从第一个表中提取ID(如果不存在insert,则提取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();
  }
}

我还没有测试过它,所以我的标准免责声明可能需要一些调整

$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.
老实说,我不知道你想用path表做什么。看起来您正试图从第一个表(firsttable.ID,firsttable.data)中提取字段,这是没有联接无法完成的。如果您只是想在第二个表中查找与第一个表具有相应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,我知道我解释得不好,但第二个查询正是我要做的。不过我有一个问题。在SQL查询中,您使用了“WHERE id=?”为什么我不能使用“id=$id”?您可以,但如果使用了,请确保$id已被清除。如果这是从POST、GET或COOKIE读取的$id,则需要确保它不包含任何恶意或非法输入。您可以像上面那样使用参数化查询,也可以使用preg_match()语句来确保用户没有试图执行SQL注入攻击。只需运行查询,就得到了错误“警告:mysqli_stmt_bind_param()期望参数1为mysqli_stmt,boolean give”,对于包含mysqli_stmt_bind_param、mysqli_stmt_execute的行。出现致命错误:为mysqli_stmt_get_结果调用未定义的函数mysqli_stmt_get_result()。没错,我习惯于使用面向对象的版本,实际上我不在可以测试PHP的服务器上。我已经更新了代码片段以修复语法。非常感谢您的帮助,调整了代码,但得到了mysqli_prepare($query,$sql)的“警告:mysqli_prepare()期望参数1为mysqli,object given”