mysqli_result类的对象无法转换为字符串(搜索条件)

时间:2016-01-27 17:31:02

标签: php mysqli

我是新的PHP并尝试根据使用mysql的youtube上的教程制作标准搜索并且我尝试在mysqli中执行它...现在我收到错误类mysqli_result的对象无法转换为字符串< / strong>。错误在此行 $ result。=“AND tblpartner.companyState ='{$ location}'”; 。编写代码的正确方法是什么?下面是我的一些代码..

<?php

$result = $mysqli->query("SELECT tbljob.jobId, tbljob.title, tbljob.position1, tbljob.position2, tbljob.position3, tbljob.description1, tbljob.requirement1, tbljob.mincgpa1, tbljob.alowance1, tbljob.status, tbljob.pdate, tbljob.companyId, tblpartner.companyName, tblpartner.companyAddress, tblpartner.companyCity, tblpartner.companyState FROM tbljob INNER JOIN tblpartner ON tbljob.companyId = tblpartner.companyUsername WHERE tbljob.status = 'active' ORDER BY tbljob.pDate DESC");

if (isset($_POST['submit']))
{

  $location = mysqli_real_escape_string($mysqli, $_POST['location']);

  $result .= "AND tblpartner.companyState = '{$location}' ";

}

if ($result->num_rows != 0) {


  while($row = mysqli_fetch_array($result)) {

?>

 <div class="sub">Position: <?php echo $row['position1']; ?></div>         
 <div class="sub">Position 2 :<?php echo $row['position2']; ?></div>
 <div class="sub">Position 3 :<?php echo $row['position3']; ?><br>&nbsp;</br></div>
 //other output **trying to reduce the code
<?php
}
}
?>

这是我的搜索框

<form name = "searchForm"  method="POST" action="t.html">
                    <p><select id="location" name="location" class="form-control placeholder1">
                            <option value="" selected>All Location</option>
                            <option value="sabah">Sabah</option>
                            <option value="perak">Perak</option>
                            <option value="pahang">Pahang</option>
                        </select></p>

                    <p><input type="submit" value="Search" id="btnSubmit" name="submit" class="btn btn-success" /></p>
 </form>

2 个答案:

答案 0 :(得分:1)

在if条件被考虑之前,您正在运行查询。这就是为什么$result是一个mysqli对象的原因。您需要在查询运行之前考虑if条件。

if (isset($_POST['submit']))
{

  $location = mysqli_real_escape_string($mysqli, $_POST['location']);

  $where_sql = " AND tblpartner.companyState = '{$location}' ";

}
else
{
  $where_sql = "";
}

$result = $mysqli->query("SELECT tbljob.jobId, tbljob.title, tbljob.position1, tbljob.position2, tbljob.position3, tbljob.description1, tbljob.requirement1, tbljob.mincgpa1, tbljob.alowance1, tbljob.status, tbljob.pdate, tbljob.companyId, tblpartner.companyName, tblpartner.companyAddress, tblpartner.companyCity, tblpartner.companyState FROM tbljob INNER JOIN tblpartner ON tbljob.companyId = tblpartner.companyUsername WHERE tbljob.status = 'active'".$where_sql." ORDER BY tbljob.pDate DESC");

答案 1 :(得分:1)

您正在执行查询,然后您尝试在查询中添加内容:p,任何方式都尝试这种方式:

<?php
$result = "SELECT tbljob.jobId, tbljob.title, tbljob.position1, tbljob.position2, tbljob.position3, tbljob.description1,
 tbljob.requirement1, tbljob.mincgpa1, tbljob.alowance1, tbljob.status, tbljob.pdate, tbljob.companyId, tblpartner.companyName, 
tblpartner.companyAddress, tblpartner.companyCity, tblpartner.companyState 
FROM tbljob INNER JOIN tblpartner ON tbljob.companyId = tblpartner.companyUsername WHERE tbljob.status = 'active' ";



    if (isset($_POST['submit']))
    {

      $location = mysqli_real_escape_string($mysqli, $_POST['location']);
       $result .= "AND tblpartner.companyState = '{$location}' ";

    }

 $result .=  "ORDER BY tbljob.pDate DESC";

 $mysqli->query($result);