某些数据库条目被忽略

时间:2016-02-13 14:53:50

标签: php mysql database mysqli undefined

这件事让我很困惑,我有一个包含各种条目的数据库,对于一个特定的条目名415(也恰好是最后一个,目前在数据库中),它被忽略和对待不存在。

这是我的代码:

/* 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英寸   第46行/home/saleturbo/public_html/lifetime-new6.php    - 注意:未定义的偏移量:第47行/home/saleturbo/public_html/lifetime-new6.php中的2

     

446 - 1654.39

这是products表格的相关数据库屏幕截图:

enter image description here

如您所见,415条目具有值,其构造方式与其他条目类似。

1 个答案:

答案 0 :(得分:1)

它可能具有前导/尾随空格

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