-2

我正在运行一个 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。

我不需要随时更新数据库,我需要更新第一个查询的结果/数组。

4

3 回答 3

1

您需要做的是将结果集存储在数组中并在数组中替换。

$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();
$dataset = array();
// Store result in an array
while($assoc = mysql_fetch_assoc($r)) {
  $dataset[] = $assoc;
}
// Update array
foreach($dataset as $data) {
  $getname = mysql_query("select catname from shop_cat where id='{$data['subof']}'");
  $rowname = mysql_fetch_assoc($getname);
  // replace data
  replace_dataset($data['subof'], $rowname);
}

function replace_dataset($key, $newname) {
  global $dataset;
  foreach($dataset as $k => $data) {
    if ($data['id'] == $key)
      $dataset[$k]['subof'] = $newname;
  }
}
于 2013-03-24T04:02:20.333 回答
0

您是在问如何在 PHP 中执行此操作还是什么?如果您正在寻找如何在 PHP 中循环结果,就这么简单

$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)) {
  // the values from the query are assigned to the variables
  // $shopid, $catid, $name, $catname, $subof in that order already
  mysql_query("update shop_cat set subof=catname where id='$subof'");
  // My interpretation of your query can be wrong though
  // But you should get the idea
}
于 2013-03-23T08:19:21.957 回答
0

您可以使用 mysql_fetch_assoc() 或 mysql_fetch_array() 或 mysql_fetch_row() 函数来获取行,并可以将循环概念放在上面。

之后,您可以使用 mysql_fetch_field() 从中获取字段 subof 和 id。

然后更新数据库

您可以查看以下链接

http://www.php.net/manual/en/function.mysql-fetch-array.php

http://www.php.net/manual/en/function.mysql-fetch-assoc.php

http://www.php.net/manual/en/function.mysql-fetch-field.php

我希望你能有所了解。

于 2013-03-23T09:06:58.130 回答