doujilou3903 2016-02-16 20:49
浏览 130
已采纳

返回带有&符号的字段

I'm having a strange problem while trying to get some data out of a MySQL database using PHP. Not sure if it matters, but encoding on database, connection and PHP file are all UTF-8. Heres my code:

$testcode = "Unique12345 & TestName";
$sql="
    Select
        dw_test.testID,
        dw_test.testText
    From
        dw_test
    Where
        dw_test.testCode = '".$testcode."'
";
if(!$qry = mysqli_query($link, $sql)) reporterror(mysqli_error($link), $sql, $_SERVER['SCRIPT_URL'], __FILE__, __LINE__);
if(mysqli_num_rows($qry)>0){
    $test_array = mysqli_fetch_assoc($qry);
    $resultTest = $test_array['testID'];
    $testText = $test_array['testText'];
}else{
    echo "Nothing found";
}

If I run that using PHP, it will say "Nothing found", but if I run it using Windows MySQL Workbench, it returns 1 record (the correct one). I suspect that it is the ampersand (&), because changing the query to something else that exists works fine.

I have tried escaping the & with a slash:

$testcode = str_replace("&","\&",$testcode);

and I have tried playing around with single and double quote combinations, but everything I try produces the same result

What am I doing wrong in my PHP? How come the Workbench tool works fine when using the same query?

  • 写回答

1条回答 默认 最新

  • duanbogan5878 2016-02-16 21:45
    关注

    I solved it! I added this in front of the sql query:

    $testcode = str_replace("&","&",$testcode);
    

    Thanks for everyones help

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?