问题是这部分:
" avatar,substring_index(substring_index(wp_usermeta.meta_value ,'"',-2),'"',1) role from " .double-quoted string中有双引号。那些必须使用反斜杠进行转义:
" avatar,substring_index(substring_index(wp_usermeta.meta_value ,'\"',-2),'\"',1) role from " .但是,在构造这样的复杂字符串时,我强烈建议您使用Heredoc语法来提高可读性。它可能看起来像这样:
$utable = TABLE_PREFIX.DB_USERTABLE;
$uid = DB_USERTABLE_USERID;
$uname = DB_USERTABLE_NAME;
$atable = DB_AVATARTABLE;
$afield = DB_AVAYATFIELD;
echo <<<_>
select $utable.$uid
userid, $utable.$uname
username, $utable.$uname
link, $afield
avatar,substring_index(substring_index(wp_usermeta.meta_value ,'"',-2),'"',1)
role from $utable
left join wp_usermeta on $utable.$uid = wp_usermeta.user_id
left join cometchat_status on $utable.$uid = cometchat_status.userid $atable
where
(select count(*) from wp_bp_friends
where (initiator_user_id='$userid' and friend_user_id=wp_users.ID)
or (initiator_user_id=wp_users.ID and friend_user_id='$userid')
)=1
and wp_usermeta.meta_key = 'wp_capabilities' order by username asc
_;请注意,constants未在Heredoc中展开,因此您必须先将它们存储在变量中。
如果您愿意,可以将结束标识符(_)替换为更具可读性的标识符,例如ENDSQL或类似内容。