Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7670559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:51:00+00:00 2026-05-31T15:51:00+00:00

I am working with Moodle 2.2.1 but have a basic php-mysql problem. I am

  • 0

I am working with Moodle 2.2.1 but have a basic php-mysql problem. I am trying to return a count of a field with a condition in my WHERE clause. The WHERE clause condition is coming from a foreach loop.This is my code:

    foreach($allunits as $allunit)
    {
     $countquery = $DB->get_records_sql("SELECT COUNT(uid.userid) FROM {user_info_data} uid WHERE uid.data = '".$allunit."' OR uid.data = '".$allunit."\r'");
     $ucount = $ucount + $countquery;       
    }

This throws an unsupported operand types error. Since the ‘data’ field can have junk lines/spaces added to it I need to use \r to search for such values. If i directly pass the value in wHERE clause it works fine, like this,

    SELECT COUNT(uid.data) FROM {user_info_data} WHERE uid.data = 'Marketing' OR uid.data = 'Marketing\r'

but my wHERE clause value needs to come from an array.

Can any one help me out with this problem?

Thanks

EDIT: a var_dump of my array ‘$allunits’

array(118) {
  [0] => string(9) "PBS Group"
  [1] => string(15) "Admin-PBS Group"
  [2] => string(15) "Sales Functions"
  [3] => string(17) "Support Functions"
  [4] => string(10) "Executives"
  [5] => string(4) "Nemo"
  [6] => string(7) "Prop Co"
  [7] => string(21) "Admin-Sales Functions"
  [8] => string(7) "Connect"
  [9] => string(12) "Intermediary"
  [10] => string(6) "Retail"
  [11] => string(13) "Sales Support"
  [12] => string(15) "Business Change"
  [13] => string(13) "Group Finance"
  [14] => string(2) "HR"
  [15] => string(16) "IT and eChannels"
  [16] => string(9) "Marketing"
  [17] => string(15) "Member Services"
  [18] => string(9) "Oversight"
  [19] => string(24) "Executives and Directors"
  [20] => string(19) "Personal Assistants"
  [21] => string(19) "Corporate Property "
  [22] => string(10) "Peter Alan"
  [23] => string(8) "Premises"
  [24] => string(18) "Property Solutions"
  [25] => string(9) "Surveyors"
  [26] => string(13) "Connect Sales"
  [27] => string(15) "Connect Service"
  [28] => string(12) "Retail Sales"
  [29] => string(15) "Regulated Sales"
  [30] => string(21) "Administration-Retail"
  [31] => string(19) "Business Partnering"
  [32] => string(21) "Capital and Liquidity"
  [33] => string(18) "Finance Operations"
  [34] => string(11) "Procurement"
  [35] => string(15) "Risk Management"
  [36] => string(8) "Treasury"
  [37] => string(8) "Group HR"
  [38] => string(9) "Group LnD"
  [39] => string(9) "eChannels"
  [40] => string(29) "eChannels Sales and Marketing"
  [41] => string(15) "IT Architecture"
  [42] => string(11) "IT Delivery"
  [43] => string(11) "IT Services"
  [44] => string(19) "Business Operations"
  [45] => string(11) "Collections"
  [46] => string(23) "Mortgage Administration"
  [47] => string(3) "MSC"
  [48] => string(8) "Registry"
  [49] => string(7) "Savings"
  [50] => string(22) "Group Business Conduct"
  [51] => string(32) "Group Financial Crime Prevention"
  [52] => string(26) "Group Information Security"
  [53] => string(14) "Group Internal"
  [54] => string(14) "Legal Services"
  [55] => string(11) "Secretariat"
  [56] => string(25) "Administration-Peter Alan"
  [57] => string(7) "Agency "
  [58] => string(8) "Lettings"
  [59] => string(17) "Retail Sales City"
  [60] => string(17) "Retail Sales East"
  [61] => string(18) "Retail Sales North"
  [62] => string(18) "Retail Sales South"
  [63] => string(17) "Retail Sales West"
  [64] => string(21) "Regulated Sales South"
  [65] => string(20) "Regulated Sales West"
  [66] => string(29) "Albany Road-Retail Sales City"
  [67] => string(23) "Barry-Retail Sales City"
  [68] => string(28) "Birchgrove-Retail Sales City"
  [69] => string(24) "Canton-Retail Sales City"
  [70] => string(26) "Llandaff-Retail Sales City"
  [71] => string(25) "Penarth-Retail Sales City"
  [72] => string(27) "Llanishen-Retail Sales City"
  [73] => string(30) "Queen Street-Retail Sales City"
  [74] => string(24) "Rumney-Retail Sales City"
  [75] => string(31) "St Davids two-Retail Sales City"
  [76] => string(28) "Whitchurch-Retail Sales City"
  [77] => string(26) "Aberdare-Retail Sales East"
  [78] => string(29) "Abergavenny-Retail Sales East"
  [79] => string(27) "Blackwood-Retail Sales East"
  [80] => string(28) "Caerphilly-Retail Sales East"
  [81] => string(27) "Ebbw Vale-Retail Sales East"
  [82] => string(26) "Hereford-Retail Sales East"
  [83] => string(35) "Llandrindod Wells-Retail Sales East"
  [84] => string(32) "Merthyr Tydfil-Retail Sales East"
  [85] => string(26) "Monmouth-Retail Sales East"
  [86] => string(25) "Newport-Retail Sales East"
  [87] => string(27) "Pontypool-Retail Sales East"
  [88] => string(25) "Bangor-Retail Sales North"
  [89] => string(26) "Chester-Retail Sales North"
  [90] => string(26) "Denbigh-Retail Sales North"
  [91] => string(28) "Llandudno-Retail Sales North"
  [92] => string(23) "Mold-Retail Sales North"
  [93] => string(28) "Prestatyn-Retail Sales North"
  [94] => string(27) "Pwllheli-Retail Sales North"
  [95] => string(29) "Shrewsbury-Retail Sales North"
  [96] => string(26) "Wrexham-Retail Sales North"
  [97] => string(27) "Bridgend-Retail Sales South"
  [98] => string(28) "Cowbridge-Retail Sales South"
  [99] => string(33) "Llantwit Major-Retail Sales South"
  [100] => string(26) "Maesteg-Retail Sales South"
  [101] => string(24) "Neath-Retail Sales South"
  [102] => string(29) "Pontypridd-Retail Sales South"
  [103] => string(28) "Porthcawl-Retail Sales South"
  [104] => string(30) "Port Talbot-Retail Sales South"
  [105] => string(28) "Tonypandy-Retail Sales South"
  [106] => string(27) "Treorchy-Retail Sales South"
  [107] => string(29) "Aberystwyth-Retail Sales West"
  [108] => string(28) "Carmarthen-Retail Sales West"
  [109] => string(27) "Fishguard-Retail Sales West"
  [110] => string(27) "Gorseinon-Retail Sales West"
  [111] => string(31) "Haverfordwest-Retail Sales West"
  [112] => string(27) "Llampeter-Retail Sales West"
  [113] => string(27) "Llandeilo-Retail Sales West"
  [114] => string(26) "Llanelli-Retail Sales West"
  [115] => string(27) "Morriston-Retail Sales West"
  [116] => string(25) "Mumbles-Retail Sales West"
  [117] => string(25) "Swansea-Retail Sales West"
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:51:02+00:00Added an answer on May 31, 2026 at 3:51 pm

    You are passing a literal carriage return, instead of the string \r which is what is required. This is because your string is enclosed in double quotes and PHP is interpreting the escape sequence before it is passed to MySQL.

    The solution is simple – either enclose your SQL string in single quotes, or even simpler, escape the backslash with another backslash:

    $countquery = $DB->get_records_sql("SELECT COUNT(uid.userid) FROM {user_info_data} uid WHERE uid.data = '".$allunit."' OR uid.data = '".$allunit."\\r'");
    

    EDIT

    I think this will do what you want in a single query with no looping:

    $query = "
      SELECT COUNT(uid.data) AS users
      FROM {user_info_data}
      WHERE uid.data IN ('".implode("','", $allunits)."','".implode("\\r','", $allunits)."\\r')
    ";
    $countquery = $DB->get_records_sql($query);
    $ucount = $countquery->users;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on Moodle but have a php-mysql related doubt which is turning
I am working on Moodle 2.2.1 and have created dynamic.csv files using certain queries.
I am working with the moodle system, but it turns out that it uses
Working with some basic java apps on CentOS 5 linux and I have my
Im using jQuery/PHP/MySql to load twitter search results on the page but limited to
I'm working on a new Moodle Assignment plugin. How can I include a custom
Working on calling a C function from my asm project. I'm trying to push
Working through the five minute Xtext tutorial (http://www.eclipse.org/Xtext/documentation/2_1_0/010-xtext-in-5-minutes.php) I get to Generating The Language
Working example: http://alpha.jsfiddle.net/gTpWv/ Both of the methods work separately, but once regexp for smilies
Working with a Lucene index, I have a standard document format that looks something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.