1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved mysql_fetch_array throw error

Discussion in 'Development' started by Kyd, Dec 28, 2017.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    PHP:
     public function getTopPlayers(){
            
    $result self::getConnection()->query('SELECT name FROM players ORDER BY level DESC LIMIT 5');
            while(
    $row mysql_fetch_array(($result))){
                
    var_dump($row);
            }
        }
    This always say function mysql_fetch_array does not exist
    What shoud I do?
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    I'm running on Debian 8 Jessie
     
  3. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You should switch over to mysqli functions which is the one php recommends (and is also faster than mysql* functions). Call mysqli_fetch_array.
     
    SOFe likes this.
  4. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Now it's without error, but now when I want to get 1,2,3,4 and 5 player, it give me only 1 player
    PHP:
     public function getTopPlayers(){
            
    $result self::getConnection()->query('SELECT name FROM players ORDER BY level DESC LIMIT 5');
            while(
    $row mysqli_fetch_array(($result))){
                
    $top = [];
                
    $top[] = $row;
                foreach(
    $top as $nwm => $key){
                    
    $text "1. " $key[0] . "2: " $key[1] . "3: " $key[2] . "4:" $key[3] . "5:" $key[4];
                    return 
    $text;
                }
            }
        }
    For $key[1,2,3,4] it's undefined offset
    But when I var_dump $row it return this
    PHP:
    array(2) {
      [
    0]=>
      
    string(7"test1"
      
    ["name"]=>
      
    string(7"test1"
    }
    array(
    2) {
      [
    0]=>
      
    string(15"test2"
      
    ["name"]=>
      
    string(15"test2"
    }
    array(
    2) {
      [
    0]=>
      
    string(15"test3"
      
    ["name"]=>
      
    string(15"test3"
    }
    array(
    2) {
      [
    0]=>
      
    string(11"test4"
      
    ["name"]=>
      
    string(11"test4"
    }
    array(
    2) {
      [
    0]=>
      
    string(7"test5"
      
    ["name"]=>
      
    string(7"test5"
    }
    What's wrong ? o_Oo_Oo_O
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.