php - 从表中选择数据,其中列像数组
问题描述
试图从数据库中获取一个数组并使用这个数组来执行一个查询,比如:“select from table where column like that array”。
在搜索了许多旧问题后,无法找到解决方案。
<?php
$mysqli = new mysqli("localhost", "hhtt", "htt", "htt");
/* Vérification de la connexion */
if ($mysqli->connect_errno) {
printf("Echec de la connexion : %s\n", $mysqli->connect_error);
exit();
}
$query = "SELECT site FROM user_info";
$result = $mysqli->query($query);
/* Tableau numérique */
while($row = $result->fetch_array())
{
$rows[] = $row;
}
$string = implode(' OR arrondissement LIKE ', $rows);
$query2 = "SELECT path FROM photos WHERE arrondissement LIKE {$string}";
$myArray = array();
$result2 = $mysqli->query($query2);
while($row2 = $result2->fetch_array(MYSQLI_ASSOC)) {
$myArray[] = $row2;
}
echo json_encode($myArray);
/*Fermeture de la connexion */
$mysqli->close();
?>
使用 mysql 查询中的数组从表中获取 json 数据。
解决方案
Looking to your code You could use a single query avoiding implode and the related select based on array
select path FROM photos p
INNER JOIN user_info u ON u.site = p.arrondissement
or if you really need like
select path FROM photos p
INNER JOIN user_info u ON p.arrondissement like concat('%', u.site, '%')
an dif you need others where condition yu jst add the where clause after the join
select path FROM photos p
INNER JOIN user_info u ON p.arrondissement like concat('%', u.site, '%')
where p.is_clean = 0
or extend the JOIN clause
select path FROM photos p
INNER JOIN user_info u ON p.arrondissement like concat('%', u.site, '%')
AND p.is_clean = 0
推荐阅读
- python - 使用 Selenium 进行 WebScraping:单击并打开按钮
- mysql - 我可以查询一个 SQL 数据库并一次合并四个表吗?
- javascript - 直接更改 CSS 样式表
- node.js - 在模型 \"user\"" 的路径 \"_id\" 处获取值 \"{ _id: 'allchatroom' }\" (type Object) 在 mongoose Cast to ObjectId 中的错误失败
- excel - VBA中的函数在hms中执行加法并推送导致单元格显示错误
- laravel - laravel + mix + vue 路由器错误:找不到导出“createStaticVNode”(导入为“_createStaticVNode”)
- javascript - 如何解决此问题“警告:失败的道具类型:道具`title`在`Tab`中标记为必填,但其值为`undefined`。”
- string - 在字符串中的每个大写字母前面放置一个空格
- case - 可以使用大小写来测试文件名中的关键文本吗?
- javascript - 使用 Ed25519 KeyPair 对 JWS(json web 签名)进行签名和验证