dongpang2483 2015-06-11 09:50
浏览 21

SQL没有正确返回表中存在的行

I have an issue with a table in a database. There are three columns as follows:

Name (VARCHAR(25))
QText (TEXT)
AText (TEXT)

My query is run through PDO as:

SELECT `AText` FROM `Exam_QA_Data` WHERE `Name` = '$name' AND `QText` = '$question'

After processing it looks like this and obviously the length of the $question variable will change:

SELECT `AText` FROM `Exam_QA_Data` WHERE `Name` = 'ExamServerTestExam' AND `QText` = 'This is the first question'

From what I can see depending on the length of the string in place of $question, sometimes it will return a row, other times it returns nothing.

If the string is short (27 chars) then it returns the 'AText' content for that row, though if the string is long (106 chars) nothing if $question is a long string. I checked the entry in the database and the entire string is present, so the row is there exactly as it is being searched for.

I have also tried using:

SELECT `AText` FROM `Exam_QA_Data` WHERE `Name` = '$name' AND `QText` LIKE '%$question%'

Can't understand if this is a database issue or not and if so why? It appears to be a problem with the length of the input string.

Thank you very much.

  • 写回答

1条回答 默认 最新

  • douzhi1972 2015-06-11 10:37
    关注

    So after some tests, I discovered that the search doesn't like a string that is over 80 characters long. In order to do this but still get the same result I input the string with a wildcard as shown in the following codeblock:

    function grab_ans_given_ques($conn, $question, $examName){
    
        // Split the string here into two parts and join with a wildcard (%)
        $ques = substr($question, 0, 20)."%".substr($question, -20, strlen($question));
        $sql  = $conn->prepare("SELECT `AText` FROM `Exam_QA_Data` WHERE `Name` = '$examName' AND `QText` LIKE '%$ques%'");
        $sql->execute();
    

    Thank you all who gave some advice or tried to help with this!

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!