Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Strange Distinct SQL / PHP Behaviour

  • 23-02-2013 8:53pm
    #1
    Registered Users, Registered Users 2 Posts: 8,004 ✭✭✭


    Hi Folks,

    Bit of an issue with my PHP / SQL here. This is the code:
    $query = "SELECT DISTINCT student FROM classes LIMIT 100";
    $result = mysqli_query($link, $query);
    $row = mysqli_fetch_array($result, MYSQLI_NUM);
    print_r($row);
    

    Having run the query manually I get about 40 returned values. All good there. However when I do the print_r, I'm only getting the first returned value i.e. The $row array only has one entry. Link and db are fine, it just appears to be my array handling.

    Any Thoughts?


Comments

  • Registered Users, Registered Users 2 Posts: 8,004 ✭✭✭ironclaw


    Racked my brains, you need to loop:
    while($row = mysqli_fetch_array($result, MYSQLI_NUM))
    {
        print_r($row);
    }
    


  • Registered Users, Registered Users 2 Posts: 2,781 ✭✭✭amen


    You may have a bigger problem.

    [PHP]SELECT DISTINCT student FROM classes LIMIT 100[/PHP]

    may return a random set of 100 students each time the sql is run.

    If thats what you want fine but if not you should consider adding an order by clause.


  • Registered Users, Registered Users 2 Posts: 8,004 ✭✭✭ironclaw


    amen wrote: »
    You may have a bigger problem.

    [PHP]SELECT DISTINCT student FROM classes LIMIT 100[/PHP]

    may return a random set of 100 students each time the sql is run.

    If thats what you want fine but if not you should consider adding an order by clause.

    Sorry, that was an example only. I was limiting it for debug purposes. Its a good point however!


  • Closed Accounts Posts: 249 ✭✭OneIdea


    ironclaw wrote: »
    Racked my brains, you need to loop:
    while($row = mysqli_fetch_array($result, MYSQLI_NUM))
    {
        print_r($row);
    }
    


    Pretty much the same thing with a condition and clear/close your connections after queries.
    [php]

    // I normally have an include database connection file here

    $query = "SELECT DISTINCT student FROM classes LIMIT 100";

    $result = $mysqli->query($query);

    if ($result->fetch_array(MYSQLI_NUM) == 0 ){
    // do something if nothing found
    } else {
    while ($row = $result->fetch_array(MYSQLI_NUM)){
    print_r($row)
    }
    }

    $result->free();
    $mysqli->close();
    [/php]

    EDIT: Just to add, although it is very rare, I went to school with a student of the same name as mine. Perhaps you might consider altering your query to include something extra like $query = "SELECT DISTINCT student, DATE of BIRTH FROM classes LIMIT 100";

    [php]

    // I normally have an include database connection file here

    $query = "SELECT DISTINCT student, dob FROM classes LIMIT 100";

    $result = $mysqli->query($query);

    if ($result->fetch_array(MYSQLI_NUM) == 0 ){
    // do something if nothing found
    } else {
    while ($row = $result->fetch_array(MYSQLI_ASSOC)){
    print_r($row)
    // print_r($row)
    }
    }

    $result->free();
    $mysqli->close();
    [/php]


Advertisement