"select " . TABLE_PREFIX.DB_USERTABLE . "." . DB_USERTABLE_USERID .
" userid, " . TABLE_PREFIX.DB_USERTABLE . "." . DB_USERTABLE_NAME .
" username, " . TABLE_PREFIX.DB_USERTABLE . "." . DB_USERTABLE_NAME .
" link, " . DB_AVATARFIELD .
" avatar,substring_index(substring_index(wp_usermeta.meta_value ,'"',-2),'"',1) role from " .
TABLE_PREFIX.DB_USERTABLE .
"left join wp_usermeta on " . TABLE_PREFIX.DB_USERTABLE .
"." . DB_USERTABLE_USERID .
"= wp_usermeta.user_id left join cometchat_status on " .
TABLE_PREFIX.DB_USERTABLE . "." . DB_USERTABLE_USERID .
" = cometchat_status.userid " . DB_AVATARTABLE .
" 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"
子字符串有错误如何避免。
答案 0 :(得分:1)
问题在于声明的这一部分:
" avatar,substring_index(substring_index(wp_usermeta.meta_value ,'"',-2),'"',1) role from "
您正在使用"
来分隔PHP字符串,但在其中您尝试使用包含"
的SQL字符串。嵌入的引用是终止PHP字符串。您需要转义嵌入式引号:
" avatar,substring_index(substring_index(wp_usermeta.meta_value ,'\"',-2),'\"',1) role from "
答案 1 :(得分:1)
MySQL语句中有"
个双引号。这些由PHP解释为关闭字符串,因此您需要转义它们:\"
"select ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." userid, ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_NAME." username, ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_NAME." link, ".DB_AVATARFIELD." avatar,substring_index(substring_index(wp_usermeta.meta_value ,'\"',-2),'\"',1) role from ".TABLE_PREFIX.DB_USERTABLE."left join wp_usermeta on ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID."= wp_usermeta.user_id left join cometchat_status on ".TABLE_PREFIX.DB_USERTABLE.".".DB_USERTABLE_USERID." = cometchat_status.userid ".DB_AVATARTABLE." 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"
答案 2 :(得分:1)
您必须在此代码中转义双引号:
"avatar,substring_index(substring_index(wp_usermeta.meta_value ,'"',-2),'"',1) role from "
将其转换为:
"avatar,substring_index(substring_index(wp_usermeta.meta_value ,'\"',-2),'\"',1) role from "
您的代码中还有另一个短语,您必须在其中转义双引号。你必须像这样逃避它,仔细看看你会设法做到这一点。
答案 3 :(得分:1)
问题在于这一部分:
" 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语法来提高可读性。它可能看起来像这样:
<?php
$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
或类似内容。