duanmanpi9358 2016-09-29 17:05
浏览 27
已采纳

将行与数组中的元素进行匹配

I have a sql request to do with a php array to find all the occurrences of a search bar.

I can't get this done. This is my code :

    $texte = $_POST['texte'];
    $texte = explode(" ", $texte);
    $query_parts = array();

    //Proximité
    foreach ($texte as $value) {
        $value = "%".$value."%";
    }



    $sql = "SELECT * FROM PROXIMITE WHERE titrefr LIKE ':texte'";
    $req = $bd->requete_obj($sql, array('texte'=>$texte));

    while($select = $req->fetch()){
        error_log('Test');
    }
  • 写回答

1条回答 默认 最新

  • douhuantui6259 2016-09-29 17:12
    关注

    Your foreach-loop does not actually modify the variables, because they are passed by Value - meaning a copy. to modify them, you have to pass them by reference:

    foreach ($texte as &$value) {
        $value = "%".$value."%";
    }
    

    that way, the actual values in your array are being manipulated.

    secondly: you can not give your database an array as parameter. instead, you could loop through the array. if you implement that in your first loop, you don't even have to use references anymore:

    $sql = "SELECT * FROM PROXIMITE WHERE titrefr LIKE :texte";
    foreach ($texte as $value) {
        $value = "%".$value."%";
        $req = $bd->requete_obj($sql, array('texte'=>$value));
        while($select = $req->fetch()){
            error_log('Test');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏