簡體   English   中英

搜索功能輸出不正確? (PHP)

[英]search function is not outputing correct ? (PHP)

嘿,我當前正在使用搜索功能,我希望它顯示表格上有多少受影響的行,然后回顯它。 但是我的代碼可以正常工作,但是輸出錯誤。

編輯:現在它只顯示0和搜索結果,我想讓它在表acksearch中搜索他們想要的東西,然后顯示有多少結果,然后輸出並顯示所有匹配的結果。

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die("Could not connect: " . mysql_error());
}

mysql_select_db("acksocial");
mysql_query("SELECT * FROM Acksearch WHERE name = name ");
$rc = mysql_affected_rows();
echo "There was  " . $rc .        "    results of your search!";


mysql_close($con);
?>

現在它只顯示0並搜索結果,我想讓它在表acksearch中搜索他們想要的東西,然后顯示有多少結果,然后輸出並顯示所有匹配的結果。

WHERE name = name將始終為true。 您是說$name嗎? 您的錯誤輸出是什么?

如果您閱讀了php手冊,就會發現mysql_affected_rowsSELECT語句無關:

通過與link_identifier關聯的最后一個INSERT,UPDATE,REPLACE或DELETE查詢獲取受影響的行數。

您要改為mysql_num_rows

從結果集中檢索行數。 該命令僅對返回實際結果集的SELECT或SHOW語句有效。 要檢索受INSERT,UPDATE,REPLACE或DELETE查詢影響的行數,請使用mysql_affected_rows()。

首先, 不要在新代碼中使用mysql_*函數 它們不再維護,已正式棄用 看到紅色框了嗎? 相反,要了解准備好的語句 ,並使用PDOMySQLi- 本文將幫助您確定哪一個。 如果您選擇PDO, 這是一個很好的教程


從查詢的外觀上不太確定...但是希望這會有所幫助

<?php
//Connect
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("acksocial");
//Query all from Acksearch where name matches $_GET['name']
$result = mysql_query('SELECT * FROM Acksearch 
                        WHERE name="'.mysql_real_escape_string($_GET['name']).'"');
//Count the rows after result was returned
$totalResults=mysql_num_rows($result);

echo "There is {$totalResults} results of your search!";
//If data is found loop it
if($totalResults>=1){
    while ($row=mysql_fetch_assoc($result)){
        echo $row['name'];
        ...
        ...
        ...
    }
}
?>
--Or--
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
mysql_select_db("acksocial");
//Count rows in the database then return that value
$result = mysql_result(mysql_query('SELECT count(1) as totalRows
                                    FROM Acksearch 
                                    WHERE name="'.mysql_real_escape_string($_GET['name']).'"'),0,'totalRows');

echo "There was {$result['totalRows']} results of your search!";
?>

嘗試這個:

mysql_query("SELECT * FROM Acksearch WHERE name = 'name'");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM