我正在使用MySQL数据库在PHP中开发一个简单的测验应用程序。我允许答案的多个输入并将其存储在MySQL数据库中的逗号分隔中,例如:
this,that
现在在数据库中,我已经保存了正确的答案,例如
this
我甚至希望用户输入的答案与我数据库中的正确答案相同。
if($resultRow['correct_answer'] == $resultRow['answer']){
$rightAns++;
}
I have also tried strcmp
but both are strict in compare.
例如
$string1 = 'this,that';
$string2 = 'this'
在上面的例子中,我想要的是因为
string1和string2具有相同的单词,称为this。
让我知道我该怎么做。谢谢!
You could
explode
theanswer
value and then see if thecorrect_answer
value is in the resulting array. For example:输出量
Demo on 3v4l.org
将多个用逗号分隔的字符串存储在一个DB列中并不是最好的处理方式。您应该将它们分别存储在单独的列或行中。
In any case, you can accomplish what you asked for like this, by splitting
$string1
into an array: