Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,912 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,731 people online right now. Registration is fast and FREE... Join Now!




While-loop trouble

 
Reply to this topicStart new topic

While-loop trouble

Moezzie
23 Mar, 2008 - 04:51 PM
Post #1

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 44


My Contributions
I started reading a little bit about the fulltext and match against features in mysql a couple of days ago. I looked at some examples and played around with the one on this site (on the verry bottom)

CODE
    $sql = "SELECT DISTINCT MATCH(name) Against ('$search' IN BOOLEAN MODE) as score, name FROM pdfdb WHERE MATCH(name) Against ('$search' IN BOOLEAN MODE) ORDER BY score DESC";
    $query = mysql_query($sql) or die(mysql_error());
    $row_sql = mysql_fetch_assoc($query);
    $total = mysql_num_rows($query);
    
echo "Number of rows found: ".$total;

    if($total>0) {
        while ($row_sql = mysql_fetch_assoc($query)) {//echo out the results
        echo ''.$row_sql['name'].'<br />';
        }
    } else
        {
        echo "No results to display";
    }

It seems like the whileloop doesnt complete its task though. It seems to leave out the first result. I added an echo to show the number of rows found and i always get one result more than is printet out afterwards.

Any ideas?
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: While-loop Trouble
23 Mar, 2008 - 05:01 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,231



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Yeah right after you run the query you put in this line...

php

$row_sql = mysql_fetch_assoc($query);


That right there eats up your first record. You fetch from the result and store it in $row_sql but then do nothing with it. Then your while loop pulls the second record when it starts. Just take out this line up top and let the while loop do all the fetching.

I hope this helps you out. Enjoy! smile.gif
User is offlineProfile CardPM
+Quote Post

Moezzie
RE: While-loop Trouble
23 Mar, 2008 - 05:14 PM
Post #3

New D.I.C Head
*

Joined: 25 Nov, 2007
Posts: 44


My Contributions
QUOTE(Martyr2 @ 23 Mar, 2008 - 06:01 PM) *

That right there eats up your first record. You fetch from the result and store it in $row_sql but then do nothing with it. Then your while loop pulls the second record when it starts. Just take out this line up top and let the while loop do all the fetching.

I hope this helps you out. Enjoy! smile.gif

That was it. It seemed odd to have this line up there to me too, i just didnt think it would do any difference exept that $row_sql would be redifined later on.
I guess i dont really understand how the mysql_fetch works quite yet. Its always worked for me so ive never questioned why or how. Gotta read up on that one.
Tanks Martyr

User is offlineProfile CardPM
+Quote Post

spearfish
RE: While-loop Trouble
23 Mar, 2008 - 05:22 PM
Post #4

Monkey in Training
Group Icon

Joined: 10 Mar, 2008
Posts: 746



Thanked: 2 times
Dream Kudos: 225
My Contributions
CODE
    $sql = "SELECT DISTINCT MATCH(name) Against ('$search' IN BOOLEAN MODE) as score, name FROM pdfdb WHERE MATCH(name) Against ('$search' IN BOOLEAN MODE) ORDER BY score DESC";
    $query = mysql_query($sql) or die(mysql_error());
    $row_sql = mysql_fetch_assoc($query);
    $total = mysql_num_rows($query);
    
echo "Number of rows found: ".$total;

    if($total>0) {
        while ($row_sql = mysql_fetch_assoc($query)) {//echo out the results
        echo ''.$row_sql['name'].'<br />';
        }
    } else
        {
        echo "No results to display";
    }


Yep, Martyr got it. mysql_fetch_assoc fetches an associative array (similar to mysql_fetch_array, which fetches an indexed array). When used on a multi-row query, it fetches the array, then moves on to the next row. That's how the while loop works, once there are no more rows to fetch the statement is false, that way it doesn't go on forever.

Also just as a sidenote, variables will expand when in double quotes. So your echo statements could become
echo "$row_sql[name] <br />";
echo "Number of rows found: $total";
etc.

Just thought I'd point that out smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:00PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month