- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Moodle 2.2.1,但有一个基本的 php-mysql 问题。我试图返回 WHERE 子句中带有条件的字段的计数。 WHERE 子句条件来自 foreach 循环。这是我的代码:
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;
}
这会引发不支持的操作数类型错误。由于“数据”字段可能添加了垃圾行/空格,因此我需要使用\r 来搜索此类值。如果我直接传递 wHERE 子句中的值,它就可以正常工作,就像这样,
SELECT COUNT(uid.data) FROM {user_info_data} WHERE uid.data = 'Marketing' OR uid.data = 'Marketing\r'
但是我的 wHERE 子句值需要来自数组。
谁能帮我解决这个问题吗?
谢谢
编辑:我的数组“$allunits”的 var_dump
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"
}
最佳答案
您正在传递一个文字回车符,而不是所需的字符串 \r
。这是因为您的字符串用双引号括起来,并且 PHP 在将其传递到 MySQL 之前解释转义序列。
解决方案很简单 - 将 SQL 字符串括在单引号中,或者更简单,用另一个反斜杠转义反斜杠:
$countquery = $DB->get_records_sql("SELECT COUNT(uid.userid) FROM {user_info_data} uid WHERE uid.data = '".$allunit."' OR uid.data = '".$allunit."\\r'");
编辑
我认为这将在单个查询中完成您想要的操作,无需循环:
$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;
关于php - 不支持的操作数类型错误/回车 - PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9804799/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!