我一直在尝试搜索,但我一直在寻找"MySQL到多维数组"问题的解决方案
我有一个多维数组,我想使用数组中的值来搜索我的MySQL数据库,并输出我需要的任何内容
我使用WHERE
子句吗?IN
条款?我需要一些帮助
我有这个阵列:
Array
(
[0] => Array
(
[id] => 333
[name] => Watches
[pluralName] => Watches
[shortName] => Watches
)
[16] => Array
(
[id] => 111
[name] => Bar
[pluralName] => Bars
[shortName] => Bar
)
)
如果我有这样的语法:
$categorySet = $dbh->prepare("SELECT c.*,ci.*,b.business_id,b.idbusiness FROM
categories c
JOIN categories_items ci ON ci.idcategory = c.idcategory
JOIN businesses b ON b.idbusiness = ci.id_items
WHERE c.id = :id OR c.name LIKE :name");
$categorySet -> bindParam(:name, $array['id']);
$categorySet -> bindParam(:name, '%'.$array['name'].'%');
$categorySet -> execute();
我想搜索MySQL,但想从数组中获得带有手表或条形图的输出。
有人能帮我吗?
谢谢!
要检查条件,您总是需要WHERE
子句,所以您必须需要它。
第二个IN
子句允许在数组为array(1,2,3,4,5);
形式时使用数组
IN示例
SELECT
*
FROM table_name
WHERE
fields IN (array(1,2,3,4,5));
更多信息
$id_sql = array();
$name_sql = array();
foreach($conditionArray as $arry)
{
$name_sql[] = $arry['name'];
$id_sql[] = $arry['id'];
}
$ids = implode(',',$id_sql);
$names = implode(',',$name_sql);
SELECT * FROM table WHERE id IN ($ids) AND name IN ($names)
如果可以的话,请为名称和ID制作单独的数组,这样您就不需要使用for-each
,只需要使用implode
,并在MySQL
语句中发送它。
<?php
$arr_queries = array ('values','values','values')
$arr_results = array();
foreach($arr as $values)
{
$sql = //do sql query here using $values,
if(mysql_num_rows($sql) != 0)
{
while($row = mysql_fetch_array($sql))
{
$temp = array();
$temp[] = $row[0]// get first column value, add other column value on $temp array
}
}
$arr_results[] = $temp;
}
?>