简体   繁体   中英

Why does my query result repeat 3 times?

the code i have below basically behaves as a friend search engine where you can add someone if you don't have them on you account. So i am trying to do the following: If exists it should just display the friend and if not then it should say add friend.

I got it to work but the tail end of the query (please see this comment "//Problem code - when i use this echo below this where it repeats 3 times") is giving me trouble where it repeats Add 3 times.

<?php
$num_rows1 = mysql_num_rows($result);
if ($result == "") {
    echo "";
}
echo "";

$rows = mysql_num_rows($result);
if ($rows == 0) {
    print("<div id=norequests>No results for <strong>$q
                 </strong></div>");

}
elseif ($rows > 0) {
    while ($row = mysql_fetch_array($query))
    {

        $person  = htmlspecialchars($row['full_name']);
        $linksys = htmlspecialchars($row['name']);
        $pid     = htmlspecialchars($row['system_id']);
    }

    print("");
}

}


else{
    echo '<div id="error">No results.</div>';
}

$sql    = "SELECT `Friend_id` from `friends_container` 
                 WHERE `System_id` = '$sid'";
$result = mysql_query($sql);
$query = mysql_query($sql) or die("Error: " . mysql_error());

if ($result == "") {
    echo "";
}
echo "";

$rows = mysql_num_rows($result);
if ($rows == 0) {
    print("");

}
elseif ($rows > 0)
{
    while ($row = mysql_fetch_array($query))
    {
        $existing = htmlspecialchars($row['Friend_id']);
        if ($existing == $pid) {
            echo("<img src=$linksys />$person - Already Existing");
        }

        else
            //Problem code - when i use this echo below this where it repeats 3 times
        {
            echo("Add $person");
        }
    }
?>

You must have 3 images stored for each account .

Your query will always result in a multiple result. what you should do is use php to convert it to something which will result in a better option or say convert the result in an array for convenience.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM