search function is not outputing correct ? (PHP) Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of search function is not outputing correct ? (PHP) without wasting too much if your time.

The question is published on by Tutorial Guruji team.

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!";
?>
We are here to answer your question about search function is not outputing correct ? (PHP) - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji