php - 检查mysql_query是否有相同的数组变量然后得到结果

时间:2016-07-16 18:21:06

标签: php arrays

我想做一个陷阱,如果数组在数据库中的值相同。然后他们将进入我构建的if条件。到目前为止,这是我的代码。

    $a = $_POST['a'];
    $b = $_POST['b'];
    foreach ($_POST['b'] as $b) 
{              
    $result = mysql_query("select * from ehr_schedule3 where (sched3_time='$b' and sched2_id='$a') ");

            $row = mysql_fetch_array($result);
            $num = mysql_num_rows($result);
}
            if(strpos($b, $result)==true) //$b is the array value I will insert to the db.
{
            header("Location: ../edit_sched2.php?asdfghjkl=".$a."?false");
                $_SESSION["A"] = "E" ;
}

我不知道如何在mysql_query中使用数组。如果只有一个相同的值,我只想继续if。我最初计划将其设为字符串,以便strpos确认它具有相同的值,然后它将继续进入if

1 个答案:

答案 0 :(得分:0)

我想有一天你可以在查询中使用DISTINCT子句,这样你就不会得到重复项。顺便说一句,您应该使用mysqli,因为不推荐使用mysql_functions。我想评论澄清,所以我可以帮助你更多,但我还没有50个声誉。所以对于我的理解到目前为止,这就是我要做的事情:

    $a = $_POST['a'];
    $b = $_POST['b'];
    $query = "SELECT schedule3_time, schedule2 FROM ehr_schedule3  WHERE sched3_time LIKE $a and sched2_time LIKE $b "
    $key = mysqli_connect($host, $user, $passwd, $database)
     or die(mysql_error); /*your credientials */
    foreach ($_POST['b'] as $b) 
      {              
            $result = mysqli_query($key, $query);

            $row = mysqli_fetch_array($result);
            $num = mysqli_num_rows($result);
            }
            if(strpos($b, $result)==true) {
             $sql = "INSERT INTO tablename (firstname, lastname, email)
            VALUES ('John', 'Doe', 'john@example.com')"; /* put your table here*/

           if (mysqli_query($key, $query)) {
                 echo "New record created successfully";
               } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
               }

             $key->close();
    } //$b is the array value I will insert to the db.
{
            header("Location: ../edit_sched2.php?asdfghjkl=".$a."?false");
                $_SESSION["A"] = "E" ;
} 

查看here you have a full C# example of a working quicksort method以帮助您解决问题。