某些数据库条目被忽略
Certain DB entry gets ignored
这件事让我很困惑,我有一个包含各种条目的数据库,对于一个特定的条目名称 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
Notice: Undefined offset: 2 in
/home/saleturbo/public_html/lifetime-new6.php on line 46
- Notice: Undefined offset: 2 in /home/saleturbo/public_html/lifetime-new6.php on line 47
446 - 1654.39
这是 products
table 的相关数据库截图:
如您所见,415
条目具有值,其构造与其他条目一样。
它可能有 leading/trailing space
while($row = $result->fetch_assoc()) {
$product_name = trim($row['Name']);
//rest of the code..
}
这件事让我很困惑,我有一个包含各种条目的数据库,对于一个特定的条目名称 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
Notice: Undefined offset: 2 in /home/saleturbo/public_html/lifetime-new6.php on line 46 - Notice: Undefined offset: 2 in /home/saleturbo/public_html/lifetime-new6.php on line 47
446 - 1654.39
这是 products
table 的相关数据库截图:
如您所见,415
条目具有值,其构造与其他条目一样。
它可能有 leading/trailing space
while($row = $result->fetch_assoc()) {
$product_name = trim($row['Name']);
//rest of the code..
}