php Mysql中的数组在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9435225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:49:07  来源:igfitidea点击:

Array in Mysql WHERE LIKE?

phpmysqlarrays

提问by sg552

I need to SELECT with array list. $array_namecontains:

我需要使用数组列表进行 SELECT。$array_name包含:

Array ( [0] => gum.cn [1] => lol.com. [2] => ns1.blar.com [3] => test.com [4] => web.cn. )

print_r($array_name);

  $string = implode(',',$array_name);


    $tank = "SELECT url FROM `PHP`.`db` WHERE url LIKE '%{$string}%'";
    $result1 = mysql_query($tank); 
      while ($jwp = mysql_fetch_array($result1)) 
      {     
      echo $jwp['url']; 
      echo "<br>"; 
      }

print_r($array_name);

  $string = implode(',',$array_name);


    $tank = "SELECT url FROM `PHP`.`db` WHERE url LIKE '%{$string}%'";
    $result1 = mysql_query($tank); 
      while ($jwp = mysql_fetch_array($result1)) 
      {     
      echo $jwp['url']; 
      echo "<br>"; 
      }

Why don't the above work? I search other example and the question is asking without using LIKEclause so no solution there. Please help, thanks in advance.

为什么以上不起作用?我搜索其他示例,问题是在不使用LIKE子句的情况下询问,因此那里没有解决方案。请帮忙,提前致谢。

回答by netcoder

It doesn't work because your query will expand to:

它不起作用,因为您的查询将扩展为:

SELECT url FROM `PHP`.`db` WHERE url LIKE '%gum.cn,lol.com.,ns1.blar.com...%'

You have to modify your query a little:

您必须稍微修改您的查询:

$query_parts = array();
foreach ($array_name as $val) {
    $query_parts[] = "'%".mysql_real_escape_string($val)."%'";
}

$string = implode(' OR url LIKE ', $query_parts);

$tank = "SELECT url FROM `PHP`.`db` WHERE url LIKE {$string}";

回答by antelove

/* Data in Array */

/* 数组中的数据 */

$array_name = array("gum.cn","lol.com","ns1.blar.com","test.com","web.cn");

/* Join array elements into a string with separator (whitespace) " " */

/* 将数组元素连接成一个带有分隔符(空格)“”的字符串 */

echo $string = implode(" ", $array_name); 
// output: gum.cn lol.com ns1.blar.com test.com web.cn

/* replace separator (whitespace) " " with "%' OR '%" */

/* 用“%' OR '%”替换分隔符(空格)“” */

echo $string = str_replace(" ", "%' OR '%", $string);
// output: gum.cn%' OR '%lol.com%' OR '%ns1.blar.com%' OR '%test.com%' OR '%web.cn

/* Insert into query sql */

/* 插入查询 sql */

$tank = "SELECT url FROM `PHP`.`db` WHERE url LIKE 

'%$string%'

“%$字符串%”

";

";

回答by ralfe

It won't work because it will try and match the database value of (for example) 'gum.cn' with '%gum.cn,lol.com,.....%', which will never be true.

它不会工作,因为它会尝试将(例如)'gum.cn' 的数据库值与 '%gum.cn,lol.com,.....%' 匹配,这永远不会是真的。

Rather do this:

而是这样做:

foreach ($array_name as $string) {
    $tank = "SELECT url FROM `PHP`.`db` WHERE url LIKE '%{$string}%'";
    $result1 = mysql_query($tank); 
    echo $jwp['url']; 
    echo "<br>"; 
}

Hope that helps.

希望有帮助。

回答by JohnFx

Because it generates invalid SQL (Okay, technically valid, it just isn't going to do what you think it will)

因为它会生成无效的 SQL(好吧,从技术上讲是有效的,它只是不会按照您的想法行事)

SELECT url FROM `PHP`.`db` WHERE url LIKE '%a,b,c,d,%'

You are either going to have to append multiple LIKE statements. Implode isn't going to work for this task unless you are doing an exact string match query. Even then you'd need to tweak the code.

您将不得不附加多个 LIKE 语句。除非您正在执行精确的字符串匹配查询,否则 Implode 不适用于此任务。即使这样,您也需要调整代码。

回答by lamplightdev

Use the IN clause, but ensure that the values are quoted and escaped:

使用 IN 子句,但确保值被引用和转义:

//TODO: escape $array_name values

$string = implode("','",$array_name); //ensure quoted values

$tank = "SELECT url FROM `PHP`.`db` WHERE url IN ('$string')";

回答by André Mendon?a

/** * Implode a multidimensional array of values, grouping characters when different with "[]" block. * @param array $array The array to implode * @return string The imploded array */ function hoArray2SqlLike( $array ) { if ( ! is_array( $array ) ) return $array;

/** * 内爆值的多维数组,当与“[]”块不同时将字符分组。* @param array $array 要内爆的数组 * @return string 内爆的数组 */ function hoArray2SqlLike( $array ) { if ( !is_array( $array ) ) return $array;

$values  = array();
$strings = array();

foreach ( $array as $value )
{
    foreach ( str_split( $value ) as $key => $char )
    {
        if ( ! is_array( $values[ $key ] ) )
        {
            if ( isset( $values[ $key ] ) )
            {
                if ( $values[ $key ] != $char )
                {
                    $values[ $key ]     = array( $values[ $key ] );
                    $values[ $key ][]   = $char;
                }
            }
            else
                $values[ $key ] = $char;
        }
        elseif ( ! array_search( $char , $values[ $key ] ) )
            $values[ $key ][] = $char;
    }
}

foreach ( $values as $value )
{
    if ( is_array( $value ) )
        $value = '['.implode( '', $value ).']';

    $strings[] = $value;
}

return implode( '', $strings );

}

}