我正在尝试从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
答案 0 :(得分:0)
您可以使用此查询。
$sql = "SELECT ID, style, region FROM mytab WHERE style LIKE '%".$style."%' and region='".$region."'";