SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Query problem - return info with - and without - loop

    Hi everyone,

    I have a query which doesn't work.

    PHP Code:
    <?php
    require (MYSQL);
    $q "SELECT col1, col2 FROM table1";
    $r = @mysqli_query($dbc$q);   
      
    $num mysqli_num_rows($r);
    if (
    $num 0) { 
    $row mysqli_fetch_array($rMYSQLI_ASSOC); 
    Print
    '<p>print something  no loop</p>'

    while (
    $row mysqli_fetch_array($rMYSQLI_ASSOC)) {
        
    Print
    '<p>print something else  must be looped</p>';

    }
    }

    ?>

    Some of the information returned from the database needs to be printed out without using a loop, and some information must be looped.

    Would the above query work at all, or must I use 2 separate queries?



    Thanks a lot for your advice.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Let's start by adding an error check to find out why your query doesn't work. That's a necessary step. There's nothing obvious in your query statement, so it's probably your connection. Do you have error checking turned on in your connection code?

    After that - why does it matter if you grab the query results using a loop or not? YOu have to get all the data somehow.. IF you use the array retrieval then you still have to loop/walk through the array to see the results. Or am I missing something?

  3. #3
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi ginerjm,

    thanks for replying. It seems that the code does work to some extent. The first block to be echoed out works fine but for some reason the second block only returns 3 rows, when it should be returning 4 rows. The row that isn't being returned is the first row. When I remove the first block, save, and refresh the page, all four rows in the second block are returned.

    Not sure if there's some conflict.

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,386
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi RedBishop,

    You should be able to use the mysqli_data_seek function to reset the result pointer back to the first record, so you can loop over the result set from the beginning:
    PHP Code:
    $query "SELECT col1, col2 FROM table1";
    $result = @mysqli_query($dbc$query);   
      
    $num mysqli_num_rows($result);

    if (
    $num 0)

        
    $row mysqli_fetch_array($resultMYSQLI_ASSOC); 
        Print 
    '<p>print something  no loop</p>'
        
        
    mysqli_data_seek($result0);

        while (
    $row mysqli_fetch_array($resultMYSQLI_ASSOC))
        {
            Print 
    '<p>print something else  must be looped</p>';
        }


  5. #5
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So - your query WAS working, your problem was in displaying the results. Fretburner has provided you the solution to your real problem.

    We would be remiss in not pointing out that the "MySQL" extension is deprecated (not 'depreciated' as so many people spell it) and you should really focus on choosing one of the other options - mysqli or pdo.

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,386
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by ginerjm View Post
    We would be remiss in not pointing out that the "MySQL" extension is deprecated (not 'depreciated' as so many people spell it) and you should really focus on choosing one of the other options - mysqli or pdo.
    RedBishop is already using the mysqli_* functions.. good point about the spelling though, I always get that one wrong.

  7. #7
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi fretburner,

    thank you for replying and I hope you're well!

    I have yet to try out your solution because of other queries giving me trouble.

    Will keep you posted.

    Cheers

  8. #8
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    funny how one can miss that little tiny i ....... oh, well.

  9. #9
    SitePoint Addict
    Join Date
    Jul 2013
    Location
    South of the equator, then turn left
    Posts
    360
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi there fretburner,

    the mysqli_data_seek function has solved the problem. Thanks a lot!

    Your advice is always helpful.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •