使用多个数组

时间:2018-02-07 11:45:35

标签: php mysql sql arrays

我正在尝试完成我已经工作了几天的变化价格更新程序。我还是非常新的PHP。

我坚持最后的更新顺序。我没有收到任何错误;但没有任何更新 - 大概是因为SQL查询返回0值。

任何帮助都非常感激。我确信这很简单。

期望的结果:在wp_postmeta中更新了_price,用于从post_parent记录中获取的_price最初检索到的post_id记录。循环播放数组中的所有记录。

最终显示已检索数据的示例数据:

$ postids(721,735,749,807)

$ parentids(714,740,742,815)

$ price(11.04,6.32,7.69,21.00)

在一个理想的世界里,我还想在插入/更新之前加入一个公式,将_price乘以2.1。

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
//Select all POST_IDs for variation 2.1M
$sql = "SELECT post_id FROM wp_postmeta WHERE meta_value = '2-1m'";
$result = $conn->query($sql);
//Array and display POST_IDs
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["post_id"]. "<br>";
    }
} else {
   echo "0 results";
}
//Prepare POST_IDs for next query
$postids = [];
foreach ($result as $row){
    $postids[] = $row["post_id"];
}


//Use POST_IDs to select all PARENT_IDs
if (!empty($postids)) {
$sql2 = "SELECT post_parent FROM wp_posts WHERE ID IN (".implode(',',$postids).")";
}
$result2 = $conn->query($sql2);
//Array and display PARENT_IDs
if ($result2->num_rows > 0) {
    // output data of each row
    while($row2 = $result2->fetch_assoc()) {
        echo "parentid: " . $row2["post_parent"]. "<br>";
    }
} else {
    echo "0 results";
}
//Prepare PARENT_IDs for next query
$parentids = [];
foreach ($result2 as $row2){
    $parentids[] = $row2["post_parent"];
}
//Select PRICES using PARENT_IDs and META_KEY for Price
if (!empty($parentids)) {
$sql3 = "SELECT meta_value FROM wp_postmeta WHERE meta_key = '_price' AND post_id IN (".implode(',', $parentids).")";
}
$result3 = $conn->query($sql3);
if ($result3->num_rows > 0) {
    // output data of each row
    while($row3 = $result3->fetch_assoc()) {
        echo "price: " . $row3["meta_value"]. "<br>";
    }
} else {
    echo "0 results";
}
//Array and display PRICES
$prices = [];
foreach ($result3 as $row3){
    $prices[] = $row3["meta_value"];
}
//Display all retrieved data
echo "<div><p><table><tr><td> ".implode('<br>', $postids)." </td><td> ".implode('<br>', $parentids)." </td><td> ".implode('<br>', $prices)." </td></tr></table></p></div>";
//UPDATE variant prices with parent prices
foreach ($prices as $key => $postids){
    $prices = $row3["meta_value"];
    $postids = $row["post_id"];
$sqlupdate = "UPDATE wp_postmeta SET meta_value = $prices WHERE post_id = $key AND meta_key = '_price'";
$update = $conn->query($sqlupdate);
     if (!$sqlupdate)  {
        echo "error updating $prices" . mysql_error();
     }
}
$conn->close();
?>

2 个答案:

答案 0 :(得分:0)

您尝试使用所有查询结果集两次。 一旦while循环使用了查询结果集,就像到达文件的末尾,它就完成了

将您正在使用的2个循环中的所有3个合并到一个循环中,该循环同时输出并创建数组。喜欢这个

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

//Select all POST_IDs for variation 2.1M
$sql = "SELECT post_id FROM wp_postmeta WHERE meta_value = '2-1m'";
$result = $conn->query($sql);

//Array and display POST_IDs

$postids = [];
if ($result->num_rows > 0) {
    // output data of each row
    // and save in array for later use

    while($row = $result->fetch_assoc()) {
        $postids[] = $row["post_id"];
        echo "id: " . $row["post_id"]. "<br>";
    }
} else {
   echo "0 results";
}
/*
No longer needed
//Prepare POST_IDs for next query
foreach ($result as $row){
    $postids[] = $row["post_id"];
}
*/

//Use POST_IDs to select all PARENT_IDs
if (!empty($postids)) {
    $sql2 = "SELECT post_parent 
            FROM wp_posts 
            WHERE ID IN (".implode(',',$postids).")";
}

$result2 = $conn->query($sql2);

//Array and display PARENT_IDs
$parentids = [];
if ($result2->num_rows > 0) {
    // output data of each row
    while($row2 = $result2->fetch_assoc()) {
        $parentids[] = $row2["post_parent"];
        echo "parentid: " . $row2["post_parent"]. "<br>";
    }
} else {
    echo "0 results";
}

//Select PRICES using PARENT_IDs and META_KEY for Price
if (!empty($parentids)) {
    $sql3 = "SELECT meta_value 
                    FROM wp_postmeta 
                    WHERE meta_key = '_price' 
                    AND post_id IN (".implode(',', $parentids).")";
}

$result3 = $conn->query($sql3);

$prices = [];
if ($result3->num_rows > 0) {
    // output data of each row
    while($row3 = $result3->fetch_assoc()) {
        $prices[] = $row3["meta_value"];
        echo "price: " . $row3["meta_value"]. "<br>";
    }
} else {
    echo "0 results";
}

//Display all retrieved data
echo "<div><p><table><tr><td> ".implode('<br>', $postids)." </td><td> ".implode('<br>', $parentids)." </td><td> ".implode('<br>', $prices)." </td></tr></table></p></div>";

在这里,您将覆盖循环在循环中创建的$ prices数组。并使用不再存在的$row3结果集!

我担心我对数据一点也不确定,所以最后一点代码chnage是一个猜测。

//UPDATE variant prices with parent prices
foreach ($prices as $key => $postids){

    //$prices = $row3["meta_value"];
    $price = $postids

    //$postids = $row["post_id"];

    $sqlupdate = "UPDATE wp_postmeta 
                    SET meta_value = $price 
                    WHERE post_id = $key 
                    AND meta_key = '_price'";
    $update = $conn->query($sqlupdate);
    if (!$sqlupdate)  {
        echo "error updating $prices" . mysql_error();
    }
}
$conn->close();
?>

答案 1 :(得分:0)

这最终是一个简单的解决方案。正如我在问题中所说,我是一名PHP初学者。我需要进一步了解RE阵列。如果有人感兴趣,这是最终查询。

//UPDATE variant prices with parent prices
foreach ($prices as $key => $np){
    $pid = $postids["$key"];
    $sqlupdate = "UPDATE wp_postmeta 
                    SET meta_value = $np
                    WHERE post_id = $pid 
                    AND meta_key = '_price'";
    $update = $conn->query($sqlupdate);
    if(mysqli_query($conn, $sqlupdate)){
        echo "Records were updated successfully.";
    } else {
        echo "ERROR: Was not able to execute $sqlupdate. " . mysqli_error($link);
    }
}