2015-07-18 179 views
0

您好我目前有php語句中的if語句與準備語句有關的問題,無論我在檢查get_result時輸入什麼內容,我的語句總是如此。我真的不知道我在做什麼,我對PHP和準備語句的概念非常陌生。PHP檢查在if語句中準備好的語句

$searchtext= $_POST['search-box']; 
$sqlName= $connect->prepare("SELECT * FROM SearchPG WHERE PGName LIKE ? "); 
       $sqlName->bind_param("s", $searchtext); 
       $sqlName->execute(); 

       $NameResult = $sqlName->get_result(); 

       $sqltext= $connect->prepare("SELECT * FROM SearchPG WHERE PGKeywords LIKE ? "); 
       $sqltext->bind_param("s", $searchtext); 
       $sqltext->execute(); 

       $TextResult = $sqltext->get_result(); 
if($NameResult->num_rows > 0 || $TextResult->num_rows > 0){ 
         if($NameResult->num_rows > 0){ 
          echo "made it into sqlname"; 
          while($rs = $NameResult->fetch_assoc()){ 
           echo "name result"; 

           ?> 
           <div class="row resultcontent"> 
            <a href="<?php echo $rs['PGUrl'];?>"> 
             <div class="col-sm-8 searchRsltCont"> 
              <img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" /> 
              <div class="contshading"> 
              </div> 
             </div> 
             <div class="col-sm-4 searchRslttxt"> 
              <h1><?php echo "name".$rs['PGName'];?></h1> 
             </div> 
            </a> 
           </div> 
           <?php 
           $count++; 
          } 
         } 
         if($count<10){ 
          echo "exploded result"; 
          foreach($searchbit as $value){ 
           $temp = '%'.$value.'%'; 
           $sqlvalue= $connect->prepare("SELECT * FROM SearchPG WHERE PGName LIKE ? OR PGKeyword LIKE ? "); 

           $sqlvalue->bind_param("ss", $temp, $temp); 
           $sqlvalue->execute(); 
           $ValueResult = $sqlvalue->get_result(); 

           if($ValueResult->num_rows > 0){ 
            while($rs = $ValueResult->fetch_assoc()){ 
             ?> 
             <div class="row resultcontent"> 
              <a href="<?php echo $rs['PGUrl'];?>"> 
               <div class="col-sm-8 searchRsltCont"> 
                <img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" /> 
                <div class="contshading"> 
                </div> 
               </div> 
               <div class="col-sm-4 searchRslttxt"> 
                <h1><?php echo "name".$rs['PGName'];?></h1> 
               </div> 
              </a> 
             </div> 
             <?php 
             $count++; 
            } 
           } 
          } 
          while($rs = $TextResult->fetch_assoc()){ 
           ?> 
           <div class="row resultcontent"> 
            <a href="<?php echo $rs['PGUrl'];?>"> 
             <div class="col-sm-8 searchRsltCont"> 
              <img src="<?php echo $rs['PGImgUrl'];?>" class="searchRsltimg" alt="search result image" /> 
              <div class="contshading"> 
              </div> 
             </div> 
             <div class="col-sm-4 searchRslttxt"> 
              <h1><?php echo "name".$rs['PGName'];?></h1> 
             </div> 
            </a> 
           </div> 
           <?php 
           $count++; 
          } 


         } 

我都試過,如果(!$結果),如果($ result-> NUM_ROWS> 0),如果($ result-> affected_rows)等,但似乎沒有任何工作有任何人有任何想法我什麼我做錯了

+0

對不起,但請將「代碼牆」縮短爲展示此問題的最小示例。沒有人想要完成所有這些工作。 – arkascha

+0

@arkascha對不起,我清理它 – noob

回答

0

很有可能你沒有安裝「native driver」(mysqlnd),這對於get_result()方法是必需的。

cf. http://php.net/manual/en/mysqli-stmt.get-result.php

嘗試使用PDO:

<?php 

$dbh = new PDO(
    'mysql:dbname=myDatabase;host=myHost', 
    'myUsername', 'myPassword'); 

$stmt = $dbh->prepare('SELECT * FROM SearchPG WHERE PGName LIKE ? '); 
$stmt->bindParam(1, $searchtext, PDO::PARAM_STR); 
$stmt->execute(); 
$result = $stmt->fetchAll(); 
foreach($result as $row) { 
    print_r($row); 
} 
+0

我剛剛檢查了我的phpinfo.php和mysqlnd啓用,也get_result似乎工作它只是任何如果與它有關的陳述總是似乎是真的,當他們應該是假的,但如果這是真的,那麼就好像if語句根本就沒有。這很奇怪。對於PDO,你不需要安裝東西,因爲如果是這樣的話,我不能使用它,因爲我正在租用服務器。 – noob

+0

將結果集賦給$ TextResult之後,你能告訴我var_dump($ TextResult)的結果是什麼嗎?退出;'會? – 1sloc

+0

當結果爲真時,我得到這個:object(mysqli_result)#6(5){[「current_field」] => int(0)[「field_count」] => int(8)[「length」] => NULL [「num_rows」] => int(0)[「type」] => int(0)}如果它失敗,我不會得到任何結果。當它假設爲true時,NameResult獲得與之前相同的結果,但假設它爲假時,也會得到相同的結果。 – noob

0

對於任何人,也許新的PHP,準備發言,有我類似的問題,我解決我的問題,通過切換get_result更改代碼後store_result以適應store_result事情似乎完美地工作。這是我的新代碼,它仍然需要一些修復和清理,但我希望這可以幫助有需要的人。

$sqlname= $connect->prepare("SELECT PGID, PGName, PGUrl, PGImgUrl FROM SearchPG WHERE PGName LIKE ? "); 
       $sqlname->bind_param("s", $searchtext); 
       $sqlname->execute(); 
       $sqlname->store_result(); 

       $sqltext= $connect->prepare("SELECT PGID, PGName, PGUrl, PGImgUrl FROM SearchPG WHERE PGKeywords LIKE ? "); 
       $sqltext->bind_param("s", $searchtext); 
       $sqltext->execute(); 
       $sqltext->store_result(); 
if($sqlname->num_rows > 0 || $sqltext->num_rows > 0){ 
         if($sqlname->num_rows > 0){ 
          $sqlname->bind_result($id, $name, $url, $imgurl); 

          while($rs = $sqlname->fetch()){ 


           ?> 
           <div class="row resultcontent"> 
            <a href="<?php echo $url;?>"> 
             <div class="col-sm-8 searchRsltCont"> 
              <img src="<?php echo $imgurl;?>" class="searchRsltimg" alt="search result image" /> 
              <div class="contshading"> 
              </div> 
             </div> 
             <div class="col-sm-4 searchRslttxt"> 
              <h1><?php echo "name".$name;?></h1> 
             </div> 
            </a> 
           </div> 
           <?php 
           $count++; 
          } 
         } 
         if($count<10){ 


          if($words > 1){ 
          foreach($searchbit as $value){ 

           $temp = '%'.$value.'%'; 
           $sqlvalue= $connect->prepare("SELECT PGID, PGName, PGUrl, PGImgUrl FROM SearchPG WHERE PGName LIKE ? OR PGKeywords LIKE ? "); 
           if (false===$sqlvalue) { 
            die('connect failed: ' . htmlspecialchars($sqlvalue->error)); 
           } 
           $sqlvalue->bind_param("ss", $temp, $temp); 
           if (false===$sqlvalue) { 
            die('bind failed: ' . htmlspecialchars($sqlvalue->error)); 
           } 
           $sqlvalue->execute(); 
           if (false===$sqlvaue) { 
            die('execute() failed: ' . htmlspecialchars($sqlvalue->error)); 
           } 
           $sqlvalue->store_result(); 

           //$ValueResult = $sqlvalue->get_result(); 

           if($sqlvalue->num_rows > 0){ 
            $sqlvalue->bind_result($id, $name, $url, $imgurl); 
            while($rs = $sqlvalue->fetch()){ 
             ?> 
             <div class="row resultcontent"> 
              <a href="<?php echo $url;?>"> 
               <div class="col-sm-8 searchRsltCont"> 
                <img src="<?php echo $imgurl;?>" class="searchRsltimg" alt="search result image" /> 
                <div class="contshading"> 
                </div> 
               </div> 
               <div class="col-sm-4 searchRslttxt"> 
                <h1><?php echo "name".$name;?></h1> 
               </div> 
              </a> 
             </div> 
             <?php 
             $count++; 
            } 
           } 
          } 
          } 
          $sqltext->bind_result($id, $name, $url, $imgurl); 

          while($rs = $sqltext->fetch()){ 
           echo "made it into the loop"; 
           ?> 
           <div class="row resultcontent"> 
            <a href="<?php echo $url;?>"> 
             <div class="col-sm-8 searchRsltCont"> 
              <img src="<?php echo $imgurl;?>" class="searchRsltimg" alt="search result image" /> 
              <div class="contshading"> 
              </div> 
             </div> 
             <div class="col-sm-4 searchRslttxt"> 
              <h1><?php echo "name".$name;?></h1> 
             </div> 
            </a> 
           </div> 
           <?php 
           $count++; 
          } 


         } 
        }