PHP使用数组结果完成MySQL查询并将结果添加到现有数组

时间:2018-02-01 19:18:23

标签: php mysql arrays foreach where

我解决了第一次迭代后true循环停止的问题,以及通过将json_decode添加到include '/var/www/html/api/apitoken.php'; include '/var/www/html/api/secrets.php'; $data = file_get_contents('/var/www/html/api/shOrders.json'); // put the contents of the file into a variable $shOrders = json_decode($data, true); $con = mysqli_connect("127.0.0.1",$user,$pass,"api") or die('Could not connect: ' . mysqli_error()); $a=0; foreach($shOrders['varID'] AS $varID) { $sql_statement= " SELECT invTrack AS invTrack, varID AS varID, varSKU AS varSku, revelSku AS revelSku, revelInvID AS revelInvID, p.prodID AS prodID, pr.ingredient AS ing, pr.main_product AS mainProduct, pr.product AS product, i.invID AS invC, pr.qty AS QTY FROM shopifyProd sp LEFT JOIN product p ON sp.revelSku = p.sku AND p.location = 1 LEFT JOIN prodRecipe pr ON p.prodID = pr.main_product LEFT JOIN inventory i ON pr.product = i.prodID WHERE varID =" . $varID . " ; "; //connect to mysql db and process the statement $result = mysqli_query($con,$sql_statement); //var_dump($sql_statement . "<br><br>"); while($row = mysqli_fetch_assoc($result)) { echo "varID: "; print_r($row['varID']); echo "<br>"; if($varID == $row['varID']) { //set correct invID for product in revel if (!$row['invC']) { $revInvID = ( (int) $row['revelInvID']); }else{ $revInvID = ( (int) $row['invC']); } //calculate qty to change in revel if(!$row['QTY']) { $changeQTY = $shOrders['soldQTY'][$a]; }else{ $changeQTY = ($row['QTY'] * $shOrders['soldQTY'][$a]); } echo $varID . " RESULTS! -- "; print_r("varSku: " . $shOrders['varSku'][$a]); echo ", "; print_r("revelInvID: " . $row['revelInvID']); echo ", "; print_r("soldQTY: " . $shOrders['soldQTY'][$a]); echo ", "; print_r("ingredient: " . $row['ing']); echo ", "; print_r("Product: " . $row['product']); echo ", "; print_r("invC: " . $revInvID); echo ", "; print_r("changeQTY: " . $changeQTY); echo "<br><br>"; $shOrders['revProdID'][$a] = ( (int) $row['product']); $shOrders['revInvID'][$a] = ( (int) $revInvID); $shOrders['changeQTY'][$a] = ( (float) $changeQTY); if (!mysqli_query($con, $sql_statement)) { print_r(mysqli_error_list($con)); echo "<br>"; mysqli_close($con); } } } $a++; set_time_limit ( 30 ); } echo "<pre>"; print_r($shOrders); echo "</pre>"; 而不写入数组的问题。我没有意识到它将它从面向对象改为关联。

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated {
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if (!Auth::guard($guard)->check()) {
        return redirect()->guest('/login');
    }

    return $next($request);
}
}

1 个答案:

答案 0 :(得分:0)

在你运行它之前

var_dump你的查询,是否有所有预期的参数?尝试在本地数据库工具(mysql workbench,phpmyadmin)中运行它,它能做你喜欢的吗?

var_dump在每一步的变量和die(或者使用调试器一次单步执行代码1行)中的变量,看看发生了什么。

看起来你没有在查询上面声明$varID

我认为你需要搬家

foreach($shOrders->varID AS $varID) {

$sql_statement = ...行之上。