2013-04-29 15 views
0

這裏的空結果是查詢:如何捕捉一個PHP的mysqli查詢

$stmt = $dbconnect->prepare("SELECT `title`,`description`,`postid` FROM `posttd` WHERE MATCH `title` AGAINST (? IN BOOLEAN MODE)"); 
$stmt->bind_param('s', $value); 
$stmt->execute(); 

$value值是「測試1」,「其他」和「測試2」

值「其他」是一個MySQL的停用詞。所以當它通過查詢傳遞時,結果什麼也沒有。

只是想知道如何捕捉它,以便我可以將它從$value陣列中取出。

var_dump($stmt->execute());將給出bool(true)所有三個。

儘可能多的我不想過濾$value的停用詞,然後在查詢中運行它。

var_dump($stmt)$stmt->execute();後會導致以下:

測試1的var_dump

object(mysqli_stmt)#2 (9) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(3) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) } 

測試2的var_dump

object(mysqli_stmt)#2 (9) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(3) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(3) } 

其他的var_dump

object(mysqli_stmt)#6 (9) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(1) ["field_count"]=> int(3) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(2) } 

唯一的區別是object(mysqli_stmt)#6

任何想法?

+0

你可以用''包裹你的參數嗎? – Aquillo 2013-04-29 11:30:06

+0

你能解釋一下嗎?你的意思是'SELECT'title','description','postid' FROM'posttd' WHERE MATCH'title' AGAINST(「?」IN BOOLEAN MODE)'? – 2013-04-29 11:45:10

+0

什麼是mysql停用詞? – 2013-04-29 13:43:17

回答

0

使用此

if($stmt->rowCount() > 0) { 
    // do what you want here 
} 

上述聲明確認是否有數據,那麼只有它會去的條件內。

+0

rowCount()是pdo正確嗎? mysqli版本是$ stmt-> num_rows如果我是正確的,brb我會去嘗試它。 – 2013-04-29 11:33:00

+0

for mysqli嘗試if($ stmt-> field_count){} – 2013-04-29 11:36:03

+0

field_count和num_rows都返回true。我仍然無法獲得空洞的結果。任何其他想法? – 2013-04-29 11:43:59

0

你應該只使用

if($res = $stmt->get_result()) { 
    //this means query returned some rows 
} 
else { 
    //and this means the result was empty 
} 

而且,是的,它需要你的php與mysqlnd(本地驅動程序)進行編譯,否則你會得到不宣mysqli_stmt::get_result()功能錯誤或類似的東西。

0

所以我無法使用內置函數來捕捉它。相反,我做了一個簡單的測試,以獲得空的結果。那就是:

$bindarray = array('+test1','+other','+test2'); 
foreach($bindarray as $key=>$value) 
{ 
echo $value; // NOTE1: This echo prints +test1, +other, and +test2 
$stmt = $dbconnect->prepare("SELECT `title`,`description`,`postid` FROM `posttd` WHERE MATCH `title` AGAINST (? IN BOOLEAN MODE)"); 
$stmt->bind_param('s', $value); 
echo 'Bind:'.$value; // NOTE2: This echo prints +test1, +other, and +test2 
$stmt->execute(); 
echo 'Execute:'.$value; // NOTE3: This echo prints +test1, +other, and +test2 
$stmt->bind_result($rtitle,$rdescription,$rpostid); 
    while($stmt->fetch()) 
    { 
     echo 'Fetch:'.$value; // NOTE4: This echo prints +test1 and +test2 ONLY 
     if(!empty($value)) 
     { 
      if(!in_array($value, $goodvalues)) 
      { 
       array_push($goodvalues,$value); 
      } 
     } 
    } 
} 

因此,大家可以看到注4:$value沒有獲取任何東西($stmt->fetch()),所以它並沒有通過while循環,所以它並沒有得到包括$goodvalues陣列中,SO我能夠從數組中分離停用詞。

如果您能夠使用內置功能找到停用詞。分享。

謝謝