0

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880

4 Answers4

2

WHERE name = name will always evaluate to true. Did you mean $name? What is your wrong output?

brian_d
  • 11,190
  • 5
  • 47
  • 72
2

If you read the php manual, you'll see that mysql_affected_rows has nothing to do with SELECT statements:

Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier.

You want mysql_num_rows instead:

Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().

Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
1

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 MySQLi - this 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!";
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
0

Try this:

mysql_query("SELECT * FROM Acksearch WHERE name = 'name'");
The Mask
  • 17,007
  • 37
  • 111
  • 185