选择记录哪个列行等于或包含帖子中的值

时间:2018-08-08 16:48:49

标签: php mysql mysqli

我正在尝试从style数据库表中获取PHP列值等于或包含mysqli MySql中的值的记录ID,但看来我是如果与region相等或包含值,则m用LIKE '".$style."'做错了事:

 $sql = "SELECT ID, style, region FROM mytab WHERE style LIKE '".$style."' and region='".$region."'";

这里:

<?php
require 'connection.php';

$region = mysqli_real_escape_string($con, $_POST["region"]);
$style = mysqli_real_escape_string($con, $_POST["style"]);

$sql = "SELECT ID, style, region FROM mytab WHERE style LIKE '".$style."' and region='".$region."'";

$result = mysqli_query($con, $sql);
$myArray = array();
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
       $myArray[] = $row;
    }
    echo json_encode($myArray);
} else {
    echo "0 results";
}

mysqli_close($con);
?>

如果我的MySql表内容是所需的结果:

ID | style                     | region
-----------------------------------------
0  | rock, soul, jazz          | alicante
1  | blues, trap, funk         | alicante
2  | reggae, african, jazz     | alicante

$region等于a licante$style等于jazz我想了解回波数组ID:

0
2

如果可以获取ID,如果$style等于soul funk,我不确定它是如何工作的:

0
1

1 个答案:

答案 0 :(得分:0)

您可以使用此查询。

$sql = "SELECT ID, style, region FROM mytab WHERE style LIKE '%".$style."%' and region='".$region."'";