php查询中正确的字符串语法


Proper syntax of strpos in php query

在php中,我有一行代码,查询数据库以查找自用户上次登录以来在聊天室内发布的消息的#。我还想为字符串查找数据中的子字符串。消息也是如此(用户的"昵称")。谁能告诉我如何正确地语法

工作
$row1 = mysql_fetch_array(mysql_query("SELECT count(d.msgid) as con,d.msgid FROM `data` as d where d.room= '".$row['room']."' and d.msgid>(select u.lastmsgnotified from user as u where u.id='".$row['id']."') order by d.msgid desc limit 0,1"));

新代码
$row2 = mysql_fetch_array(mysql_query("SELECT count(d.msgid) as con2,d.msgid FROM `data` as d where d.room= '".$row['room']."' and d.msgid>(select u.lastmsgnotified from user as u where u.id='".$row['id']."') and strpos(d.message,$row[nickname])!==false order by d.msgid desc limit 0,1"));

if(intval($row2['con2'])>1){
        sendGCM($row2['con2']." New Alerts in ".$row['room'],$row['deviceid']);
    }else if(intval($row2['con2'])>0){
    $rok = mysql_fetch_array(mysql_query("select u.*,m.* from user as u, `data` as m where u.id=m.userid and m.msgid=".$row2['msgid']));
        if($rok['nickname']=='')
        $rok['nickname'] = 'A User';
        sendGCM($rok['nickname']." Has Alerted you in room '".$rok['room']."'",$row['deviceid']);
    }

在我看来,在你的$row2=行你正在使用一个PHP函数strpos()如果你想在MySQL查询中做到这一点,你需要使用MySQL函数LOCATE()(并且你可能需要使用sub select @)。

下面是MySQL的文档:http://dev.mysql.com/doc/refman/5.0/en/string-functions.html function_locate

希望对你有帮助。

注意:您也可以查看其他MySQL字符串函数列表,看看您是否更喜欢使用其中之一:http://dev.mysql.com/doc/refman/5.0/en/string-functions.html