搜索功能输出不正确? (PHP)

时间:2011-08-04 22:40:20

标签: php html database

嘿,我现在正在弄乱搜索功能,我想让它显示它在桌子上有多少受影响的线然后回显它。但我的代码正在运行,但输出错误。

编辑:现在它只显示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中搜索他们想要的内容,然后显示有多少结果然后输出并显示所有匹配的结果。

4 个答案:

答案 0 :(得分:2)

WHERE name = name将始终评估为true。你是说$name吗?你输错的是什么?

答案 1 :(得分:2)

如果您阅读了php手册,您会发现mysql_affected_rowsSELECT语句无关:

  

通过与link_identifier关联的最后一次INSERT,UPDATE,REPLACE或DELETE查询获取受影响的行数。

您想要mysql_num_rows代替:

  

从结果集中检索行数。此命令仅对返回实际结果集的SELECT或SHOW等语句有效。要检索受INSERT,UPDATE,REPLACE或DELETE查询影响的行数,请使用mysql_affected_rows()。

答案 2 :(得分:1)

首先,Don't use mysql_* functions in new code。它们不再被维护and are officially deprecated。请参阅red box?转而了解prepared statements,并使用PDOMySQLi - this article将帮助您确定哪个。如果您选择PDO here is a good tutorial


从您的查询外观不太确定...但希望这有帮助

<?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!";
?>

答案 3 :(得分:0)

试试这个:

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