2016-02-13 72 views
0

這件事完全令我感到困惑,我有不同的條目的數據庫,對於一個特定的條目名稱415(這也恰好是最後一個,目前在數據庫中),它是越來越忽略,視爲不存在。某些DB的輸入被忽略

這裏是我的代碼:

/* database section start */ 
    $mysqli = new mysqli("localhost","user","pass","dbname"); 

    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 
/* database section end */ 

// Choose Relevant Products, and turn them into an array 
$item_array = array(
'417', 
'415', 
'446' 
); 

//implode items, turn into string 
$item_implode = join("','", $item_array); 

//declare an overall array for result 
$product = array(); 

$result = $mysqli->query("SELECT Name, WebsitePrice as price from products where Name IN ('$item_implode') ORDER BY FIELD (Name, '$item_implode');"); 

while($row = $result->fetch_assoc()) { 

    $product_name = $row['Name']; 
    // find all keys in $item_array which value is the products 
     $keys = array_keys($item_array, $product_name); 
    foreach ($keys as $key) { 

     // add values for these keys 
     $product[$key + 1]["Name"] = $row['Name']; 
     $product[$key + 1]["price"] = $row['price']; 
    } 
} 

// test print all avaialable database values 
for ($i=1; $i <= count($product)+2; $i++) { 
    echo $product[$i]["Name"] . " - "; //line 46 
    echo $product[$i]["price"] . "<br/>"; //line 47 
} 

而且我的輸出:

417 - 2588.26

注意:未定義抵消:2 /家庭/ saleturbo /的public_html/lifetime-上線46 new6.php - 通知:未定義偏移:2 /home/saleturbo/public_html/lifetime-new6.php在線47上

446 - 1654.39

這是products表的相關DB截圖:

enter image description here

正如你所看到的,415條目具有價值和構造就像其他條目。

+0

可以請你加'$ PRODUCT_NAME =修剪($行[ '名稱']);'在你的代碼,然後嘗試。 –

+0

我以前'//打印測試添加這一切avaialable數據庫values'線,一切都沒有改變。 – rockyraw

+0

你想在的'while'循環開始,你已經宣佈'$ product_name' –

回答

1

它可能是有它前/後空間

while($row = $result->fetch_assoc()) { 
    $product_name = trim($row['Name']); 
    //rest of the code.. 
}