dsm0688 2014-10-16 01:31
浏览 29

从MySQL数据库中检索转义数据

I am using StringEscapeUtils.escapeSql method of apaches lang library in my android app. I send http post requests to server to execute my SQL queries. While I am inserting the values there is no problem; I can insert the strings in escaped format.

But I can't retrieve the data from the database back to my android app. The strings that don't need to be escaped in the first place, I can retrieve with no problem. What should I do to retrieve the strings from database that I escaped before?

Here is part of my code:

PHP

case "authenticateUser":


    if ($userId = authenticateUser($db, $username, $password)) 
    {       

        $sql = "select u.Id, u.username, (NOW()-u.authenticationTime) as authenticateTimeDifference, u.IP, 
                                    f.providerId, f.requestId, f.status, u.port 
                        from friends f
                        left join users u on 
                                    u.Id = if ( f.providerId = ".$userId.", f.requestId, f.providerId ) 
                        where (f.providerId = ".$userId." and f.status=".USER_APPROVED.")  or 
                                     f.requestId = ".$userId." ";


        $sqlmessage = "SELECT m.id, m.fromuid, m.touid, m.sentdt, m.read, m.readdt, m.messagetext, u.username from messages m 
"
. "left join users u on u.Id = m.fromuid WHERE `touid` = ".$userId." AND `read` = 0 LIMIT 0, 30 ";


        if ($result = $db->query($sql))         
        {
                $out .= "<data>"; 
                $out .= "<user userKey='".$userId."' />";
                while ($row = $db->fetchObject($result))
                {
                    $status = "offline";
                    if (((int)$row->status) == USER_UNAPPROVED)
                    {
                        $status = "unApproved";
                    }
                    else if (((int)$row->authenticateTimeDifference) < TIME_INTERVAL_FOR_USER_STATUS)
                    {
                        $status = "online";

                    }
                    $out .= "<friend  username = '".$row->username."'  status='".$status."' IP='".$row->IP."' userKey = '".$row->Id."'  port='".$row->port."'/>";


                }
                    if ($resultmessage = $db->query($sqlmessage))           
                        {
                        while ($rowmessage = $db->fetchObject($resultmessage))
                            {

                            $out .= "<message  from='".$rowmessage->username."'  sendt='".$rowmessage->sentdt."' text='".$rowmessage->messagetext."' />";
                            $sqlendmsg = "UPDATE `messages` SET `read` = 1, `readdt` = '".DATE("Y-m-d H:i")."' WHERE `messages`.`id` = ".$rowmessage->id.";";
                            $db->query($sqlendmsg);
                            }
                        }
                $out .= "</data>";
        }
        else
        {
            $out = FAILED;
        }           
    }
    else
    {
            // exit application if not authenticated user
            $out = FAILED;
    }



break;

case "sendMessage":
if ($userId = authenticateUser($db, $username, $password)) 
    {   
    if (isset($_REQUEST['to']))
    {
         $tousername = $_REQUEST['to']; 
         $message = $_REQUEST['message'];   


         $sqlto = "select Id from  users where username = '".$tousername."' limit 1";



                if ($resultto = $db->query($sqlto))         
                {
                    while ($rowto = $db->fetchObject($resultto))
                    {
                        $uto = $rowto->Id;
                    }
                    $sql22 = "INSERT INTO `messages` (`fromuid`, `touid`, `sentdt`, `messagetext`) VALUES ('".$userId."', '".$uto."', '".DATE("Y-m-d H:i")."', '".$message."');";                       

                            error_log("$sql22", 3 , "error_log");
                        if ($db->query($sql22)) 
                        {
                                $out = SUCCESSFUL;
                        }               
                        else {
                                $out = FAILED;
                        }                       
                    $resultto = NULL;
                }   

    $sqlto = NULL;
    }
    }
    else
    {
        $out = FAILED;
    }   
break;

Java

public String sendMessage(String  username, String  tousername, String message) throws UnsupportedEncodingException 
{           
    String params = "username="+ URLEncoder.encode(this.username,"UTF-8") +
                        "&password="+ URLEncoder.encode(this.password,"UTF-8") +
                        "&to=" + URLEncoder.encode(tousername,"UTF-8") +
                        "&message="+ URLEncoder.encode(message,"UTF-8") +
                        "&action="  + URLEncoder.encode("sendMessage","UTF-8")+
                        "&";        
    Log.i("PARAMS", params);
    return socketOperator.sendHttpRequest(params);      
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算