search function is not outputing correct ? (PHP)

Hey im messing with a search function currently and i want it to display how many affected lines it is on the table and then echo it. but my code is working but its outputing wrong.

EDIT : Now it just displays 0 and search reseult , i wanna make it search for what they want in the table acksearch and then display how many results there was and then output it and display all that matched.

<?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);
?>

now it just displays 0 and search reseult , i wanna make it search for what they want in the table acksearch and then display how many results there was and then output it and display all that matched.

Answer

First off, Don’t use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLithis article will help you decide which. If you choose PDO, here is a good tutorial.


Not quite sure from the look of your query… but hope this helps

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

Leave a Reply

Your email address will not be published. Required fields are marked *