我正在运行一个 sql 查询,它使用内连接从两个表中提取 id、catid、name、subof。
select shop.id, shop.catid, shop.name, shop_cat.catname, shop_cat.subof from shop inner join shop_cat on shop.catid = shop_cat.id where shop.active='1' order by shop_cat.catname, shop.name
现在这会产生我需要的一切,但我需要遍历结果并对 subof 值(这是一个值,该值是 shop_cat 的 ID 号)执行另一个 sql 查询。我需要提取 subof 值 # 的 catname,然后将结果/数组字段 subof 更新为 cat 的名称。
因此,如果原始查询给我的 subof 值为 15,它会从 shop.cat 中选择 catname where id='15' 我将从该查询中获取 catname,然后为原始中的每个值更新 subof = catname具有 subof 值的结果。
编辑 3/23/13 12:30pm MST:使用更多 Opeyemi 编写的代码来解释我需要的更多内容。我不知道该怎么解释它......
$q = "select shop.id, shop.catid, shop.name, shop_cat.catname, shop_cat.subof from shop inner join shop_cat on shop.catid = shop_cat.id where shop.active='1' order by shop_cat.catname, shop.name";
$r = mysql_query();
while(list($shopid, $catid, $name, $catname, $subof) = mysql_fetch_array($r)) {
$getname = mysql_query("select catname from shop_cat where id='$subof'");
$rowname = mysql_fetch_assoc($getname);
//code to update array to change value of $subof to new $rowname['catname']
}
数据库查询运行,获取我的值。
然后我需要运行某种循环,它将遍历 PHP 从查询中获取的每个结果。此循环将获取 subof 值(它是一个整数 ID 号),然后运行查询以获取该整数值的值 catname。然后循环将更新当前结果并将 subof 值从整数更改为从循环中的数据库中提取的 catname。
我不需要随时更新数据库,我需要更新第一个查询的结果/数组。