Kallmas
Kallmas

Reputation: 97

MySQL not all results are shown

i have made i small membersearch for my site that looks for first name oder second name in my database. all members are inserted by the same way but i get not always a result back. if no member with a typed name is found than i get a message. that seams to work, but some times i get no message or if there are more than one results i get not all of them. here is my code i use.

<form id="tfnewsearch" method="POST" action="admin_search_member.php">
    <input id="tfq" class="tftextinput4" name="q" size="21" maxlength="240" value="Mitglied suchen..." />
    <input type="submit" name="startsuche" value=" " id="tfbutton4">
</form>

and that's my query

if(isset($_POST['startsuche'])) {
    $sql = "SELECT * FROM Mitglieder WHERE vorname LIKE '%".mysql_real_escape_string(trim($_POST['q']))."%' OR nachname LIKE '%".mysql_real_escape_string(trim($_POST['q']))."%'";

    $result = mysql_query($sql) OR die("<pre>\n".$sql."    </pre>\n".mysql_error()); 
    $row = mysql_fetch_array($result);
}

if (mysql_num_rows($result) == 0) {
?>
<div id="body_box_tabs">
    <div class="tabcontents">
        <div id="view1"> 
            <p style="color: #003137; font-weight: bold;">Das gesuchte Mitglied   existiert nicht!</p>
            <a href="./admin_mitglieder.php"><input class="button-link" type="submit"  value="Zurück"/></a>
        </div>
    </div>
</div>
<?php        
} else {
    echo $_POST['q'];
?>
<div id="body_box_tabs">
    <div class="tabcontents">
        <div id="view1"> 
<?php        
    while($row = mysql_fetch_array($result)) {
        echo "show me the found member";
    }
} 
?>

I hope some one can help me with this.

Upvotes: 1

Views: 85

Answers (2)

JimmyB
JimmyB

Reputation: 12610

You're basically fetching and then discarding the first row of the query result. This will cause the "does not exist" message to be shown when there are no rows, will cause no output when there's one row returned, and will cause (n-1) outputs when n>1 rows are found. You will never see the first row of the result this way.

Either remove the first $row = mysql_fetch_array($result);, or change your loop:

do {
    echo "show me the found member";
} while($row = mysql_fetch_array($result));

Upvotes: 1

Jeroen Bellemans
Jeroen Bellemans

Reputation: 2035

In this code block:

<?php        
    while($row = mysql_fetch_array($result)) {
        echo "show me the found member";
    } 
} 
?> 

Do this:

<?php        
    while($row = mysql_fetch_array($result)) { 
        echo $row['field_name'];
    }
} 
?> 

That should do the trick...

Upvotes: 0

Related Questions