嵌套的Mysqli连接

时间:2015-08-27 14:50:34

标签: php mysql sql nested

如果遇到问题请保持以下错误:

$query = ("SELECT post_ID FROM Post WHERE Post_Title LIKE'%$Main_Search%'
           AND Area_ID=$ID_Area");
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)){
    $ID_Posts= $row['post_ID'];

    $queryA =("SELECT * FROM Media 
               WHERE Post_ID =$ID_Posts 
                 AND (Date_Created BETWEEN '$DateFrom' AND '$DateTo') 
                 AND (   Media_Type = '$Film' 
                      OR Media_Type ='$Photo' 
                      OR Media_Type = '$Text' 
                      OR Media_Type = '$Audio'
                     ) 
                 AND (   Outcome_Needs = '$Needs' 
                      OR Outcome_Skills ='$Skills' 
                      OR Outcome_Difference = '$Difference' 
                      OR Outcome_Place = '$Place'");

    $result2 = mysqli_query($con, $queryA); 
    while ($row2 = mysqli_fetch_array($result2)){
         $Title = $row2["Media_Title"];
         $Desc = $row2["Media_Description"];

          echo $Title."<br/>";
          echo $Desc."<br/>";
    }
}
  

警告:mysqli_fetch_array()期望参数1为mysqli_result,   给定布尔值

这是指向while ($row2 = mysqli_fetch_array($result2))行。

我通常会说这是查询中的一个错误,但是我自己运行它并没有问题。

1 个答案:

答案 0 :(得分:0)

如果你似乎忽略了对实际信息的任何和所有要求,这是我的第一个想法。

在查询中出现问题,即我们凡人犯错误,有时MySQL或者通信也是如此......所以检查每个mysqli_函数调用的好主意已经完成没有任何打嗝。

因此,为了“教一个人钓鱼”而不是“给他一条鱼”,我添加了一些代码来检查这些mysqli电话

首先在测试时,特别是如果您在托管服务器上进行测试,最好将这两行添加到导致问题的脚本中

<?php
    error_reporting(E_ALL); 
    ini_set('display_errors', 1);

现在到你的代码

$query = ("SELECT post_ID FROM Post WHERE Post_Title LIKE'%$Main_Search%'
           AND Area_ID=$ID_Area");
$result = mysqli_query($con, $query);

if ( ! $result ) {
    echo mysqli_error($con);
    exit;
}

while ($row = mysqli_fetch_array($result)){
    $ID_Posts= $row['post_ID'];

    $queryA =("SELECT * FROM Media 
               WHERE Post_ID =$ID_Posts 
                 AND (Date_Created BETWEEN '$DateFrom' AND '$DateTo') 
                 AND (   Media_Type = '$Film' 
                      OR Media_Type ='$Photo' 
                      OR Media_Type = '$Text' 
                      OR Media_Type = '$Audio'
                     ) 
                 AND (   Outcome_Needs = '$Needs' 
                      OR Outcome_Skills ='$Skills' 
                      OR Outcome_Difference = '$Difference' 
                      OR Outcome_Place = '$Place'");

    $result2 = mysqli_query($con, $queryA); 

    if ( ! $result2 ) {
        echo mysqli_error($con);
        exit;
    }

    while ($row2 = mysqli_fetch_array($result2)){
         $Title = $row2["Media_Title"];
         $Desc = $row2["Media_Description"];

         echo $Title."<br/>";
         echo $Desc."<br/>";
    }
}

如果你在问题中发帖,任何事情都会使你无法自行解决,我会相应地修改我的答案,直到我们将你的问题提交到提交中。

错误消息的完整代码      

include("inc/db.inc.php");
$Needs = $_POST['Needs'];
if (!in_array($Needs, array('Y', 'N')))$Needs = 'N';
$Skills = $_POST['Skills'];
if (!in_array($Skills, array('Y', 'N')))$Skills = 'N';
$Difference = $_POST['Difference'];
if (!in_array($Difference, array('Y', 'N')))$Difference = 'N';
$Place = $_POST['Place'];
if (!in_array($Place, array('Y', 'N')))$Place = 'N';

If (isset($_POST['Film'])){
$Film = "Film";
}else{
$Film = "";
}
If (isset($_POST['Photo'])){
$Photo = "Photo";
}else{
$Photo = "";
}
If (isset($_POST['Text'])){
$Text = "Text";
}else{
$Text = "";
}
If (isset($_POST['Audio'])){
$Audio = "Audio";
}else{
$Audio = "";
}
$DateFrom=$_POST['Datefrom'];
$DateTo=$_POST['Dateto'];

$Main_Search = $_POST['Main_Search'];
$ID_Area = $_POST['Area_ID'];

$query = ("SELECT post_ID FROM Post WHERE Post_Title LIKE'%$Main_Search%'
       AND Area_ID=$ID_Area");
$result = mysqli_query($con, $query);

if ( ! $result ) {
echo mysqli_error($con);
exit;
}

while ($row = mysqli_fetch_array($result)){
$ID_Posts= $row['post_ID'];

$queryA =("SELECT * FROM Media 
           WHERE Post_ID =$ID_Posts 
             AND (Date_Created BETWEEN '$DateFrom' AND '$DateTo') 
             AND (   Media_Type = '$Film' 
                  OR Media_Type ='$Photo' 
                  OR Media_Type = '$Text' 
                  OR Media_Type = '$Audio'
                 ) 
             AND (   Outcome_Needs = '$Needs' 
                  OR Outcome_Skills ='$Skills' 
                  OR Outcome_Difference = '$Difference' 
                  OR Outcome_Place = '$Place'");

 $result2 = mysqli_query($con, $queryA); 

 if ( ! $result2 ) {
    echo mysqli_error($con);
    exit;
 }

while ($row2 = mysqli_fetch_array($result2)){
     $Title = $row2["Media_Title"];
     $Desc = $row2["Media_Description"];

     echo $Title."<br/>";
     echo $Desc."<br/>";
   }
}

 Notice: Undefined index: Skills in    /home/biggerstor/domains/ourbiggerstory.com/public_html/testsearch.php on line 7

 Notice: Undefined index: Difference in /home/biggerstor/domains/ourbiggerstory.com/public_html/testsearch.php on line 9

Notice: Undefined index: Place in /home/biggerstor/domains/ourbiggerstory.com/public_html/testsearch.php on line 11

您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以便在第12行的''附近使用正确的语法