php查询中strpos的正确语法

时间:2014-05-22 17:45:44

标签: php strpos

在php中,我有一行代码查询数据库,以查找自用户上次登录以来在聊天室中发布的消息数量。我还希望字符串能够查找内部的子字符串data.messages(用户"昵称")。有人可以告诉我如何正确地语法

工作

$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']);
    }

1 个答案:

答案 0 :(得分:0)

在我看来,你在$ row2 =行中使用的是PHP函数strpos() 如果你想在MySQL查询中执行此操作,则需要使用MySQL函数LOCATE()(并且您可能需要在该点使用子选择。)

这是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