write_query & fetch_array
#1
Good afternoon, everyone,

I think I don't understand something in MyBB's database methods. I want to query the entire table (including all users) mybb_users. I first tried it with a simple_select, then as follows:


    $query = $db->write_query("SELECT * FROM mybb_users");
    $usersQ = $db->fetch_array($query);

    var_dump($usersQ);


This query returns only one user (the first with UID 1) - but why not the others? There are 4 records in the mybb_users table.

Result:

array(90) {
  ["uid"]=>
  string(1) "1"
  ["username"]=>
  string(5) "admin"
  ["password"]=>
  string(32) "***"
  ["salt"]=>
  string(8) "17IGXbnv"
  ["loginkey"]=>
  string(50) "***"
  ["email"]=>
  string(17) "***"
  ["postnum"]=>
  string(2) "15"
  ["threadnum"]=>
  string(1) "3"
  ["avatar"]=>
  string(0) ""
  ["avatardimensions"]=>
  string(0) ""
  ["avatartype"]=>
  string(1) "0"
  ["usergroup"]=>
  string(1) "4"
  ["additionalgroups"]=>
  string(0) ""
  ["displaygroup"]=>
  string(1) "0"
  ["usertitle"]=>
  string(0) ""
  ["regdate"]=>
  string(10) "1560596082"
  ["lastactive"]=>
  string(10) "1561025209"
  ["lastvisit"]=>
  string(10) "1561025209"
  ["lastpost"]=>
  string(10) "1560850360"
  ["website"]=>
  string(0) ""
  ["icq"]=>
  string(1) "0"
  ["yahoo"]=>
  string(0) ""
  ["skype"]=>
  string(0) ""
  ["google"]=>
  string(0) ""
  ["birthday"]=>
  string(0) ""
  ["birthdayprivacy"]=>
  string(3) "all"
  ["signature"]=>
  string(0) ""
  ["allownotices"]=>
  string(1) "1"
  ["hideemail"]=>
  string(1) "0"
  ["subscriptionmethod"]=>
  string(1) "0"
  ["invisible"]=>
  string(1) "0"
  ["receivepms"]=>
  string(1) "1"
  ["receivefrombuddy"]=>
  string(1) "0"
  ["pmnotice"]=>
  string(1) "1"
  ["pmnotify"]=>
  string(1) "1"
  ["buddyrequestspm"]=>
  string(1) "1"
  ["buddyrequestsauto"]=>
  string(1) "0"
  ["threadmode"]=>
  string(6) "linear"
  ["showimages"]=>
  string(1) "1"
  ["showvideos"]=>
  string(1) "1"
  ["showsigs"]=>
  string(1) "1"
  ["showavatars"]=>
  string(1) "1"
  ["showquickreply"]=>
  string(1) "1"
  ["showredirect"]=>
  string(1) "1"
  ["ppp"]=>
  string(1) "0"
  ["tpp"]=>
  string(1) "0"
  ["daysprune"]=>
  string(1) "0"
  ["dateformat"]=>
  string(1) "0"
  ["timeformat"]=>
  string(1) "0"
  ["timezone"]=>
  string(1) "0"
  ["dst"]=>
  string(1) "0"
  ["dstcorrection"]=>
  string(1) "0"
  ["buddylist"]=>
  string(0) ""
  ["ignorelist"]=>
  string(0) ""
  ["style"]=>
  string(1) "0"
  ["away"]=>
  string(1) "0"
  ["awaydate"]=>
  string(1) "0"
  ["returndate"]=>
  string(1) "0"
  ["awayreason"]=>
  string(0) ""
  ["pmfolders"]=>
  string(31) "0**$%%$1**$%%$2**$%%$3**$%%$4**"
  ["notepad"]=>
  string(0) ""
  ["referrer"]=>
  string(1) "0"
  ["referrals"]=>
  string(1) "0"
  ["reputation"]=>
  string(2) "14"
  ["regip"]=>
  string(4) "�
  string(4) "XHz�"
  ["language"]=>
  string(0) ""
  ["timeonline"]=>
  string(4) "6142"
  ["showcodebuttons"]=>
  string(1) "1"
  ["totalpms"]=>
  string(1) "0"
  ["unreadpms"]=>
  string(1) "0"
  ["warningpoints"]=>
  string(1) "0"
  ["moderateposts"]=>
  string(1) "0"
  ["moderationtime"]=>
  string(1) "0"
  ["suspendposting"]=>
  string(1) "0"
  ["suspensiontime"]=>
  string(1) "0"
  ["suspendsignature"]=>
  string(1) "0"
  ["suspendsigtime"]=>
  string(1) "0"
  ["coppauser"]=>
  string(1) "0"
  ["classicpostbit"]=>
  string(1) "0"
  ["loginattempts"]=>
  string(1) "1"
  ["loginlockoutexpiry"]=>
  string(1) "0"
  ["usernotes"]=>
  string(0) ""
  ["sourceeditor"]=>
  string(1) "0"
  ["max_post_allowed"]=>
  string(1) "1"
  ["mpa_time"]=>
  string(10) "1560850331"
}

The table looks like:

   

What the hell am I doing wrong?

Thanks for help,
itsmeJAY
Reply
#2
(read db->fetch_array()).

Nothing is wrong, you just misunderstood something. After doing your query, you get a recordset, you have to loop to get all records.
What you're looking for is a fecthall() method which doesn't exists in MyBB\Db.

$query = $db->simple_select('users', '*', "1=1");
$fetchall = [];
while ($row = $db->fetch_array($query)) {
  $fetchall[] = $row;
}
var_dump($fetchall);
[MyBB 1.8 mods] - Offer me a coffee
Do not ask me help through PM
Reply
#3
I will try it. Thank you.

Perhaps the development team should consider making such a method available by default. PDO can do it too (fetchAll). Then the MyBB database class should also be able to do this.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)