这个让我绕过弯道,但是无法解决这个问题。该脚本工作正常,直到我尝试回显最终的mysqli查询(zen_id)。我已经在if和while语句之外测试了查询,它运行正常。之前的查询也没有检索$model_array['model']
的麻烦并且完美地回应它。我没有任何错误,根本没有输出。 $id_array['zen_system_id']
的vardump给我null,$id_array
也是如此,$result_2
也会以这种格式产生大量的空值:object(mysqli_result)#5 (5) { ["current_field"]=> int(0) ["field_count"]=> int(1) ["lengths"]=> NULL ["num_rows"]=> int(0) ["type"]=> int(0) }
我想我可能会误会某些东西很明显,我现在只是迷失了,可能与while函数有什么关系?
//Connect to Database
$mysqli = mysqli_connect("localhost", "login", "user", "database");
//Check Connection
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$result_1 = mysqli_query($mysqli,"SELECT `product_id` FROM oc_product");
while ($rows_1 = mysqli_fetch_array($result_1))
{
$product_id = $rows_1['product_id'];
$result = mysqli_query($mysqli,"SELECT `product_id` FROM `oc_product_option_value` WHERE `product_id`=$product_id");
$rows = mysqli_fetch_array($result);
//echo $product_id . " " . $rows['product_id'] . "</br>";
if($rows['product_id'] == null)
{
$result = mysqli_query($mysqli,"SELECT `model` FROM `oc_product` WHERE `product_id`=$product_id");
$model_array = mysqli_fetch_array($result);
$model = $model_array['model'] . "</br>";
//echo $model;
$result_2 = mysqli_query($mysqli,"SELECT `zen_system_id` FROM `oc_wholesale_link` WHERE `model_id`='" . $model . "'");
$id_array = mysqli_fetch_array($result_2);
echo $zen_id = $id_array['zen_system_id'];
}
}
答案 0 :(得分:2)
尝试封装您传递给第二个查询的数据,同时检查查询是否会返回结果,您只能为获取本身if
条件
$result = mysqli_query($mysqli,"SELECT `product_id` FROM `oc_product_option_value` WHERE `product_id`= '".$product_id."'");
if($rows = mysqli_fetch_array($result))
{
$result = mysqli_query($mysqli,"SELECT `model` FROM `oc_product` WHERE `product_id`= '".$product_id."'");
$model_array = mysqli_fetch_array($result);
$model = $model_array['model'];
//echo $model;
$result_2 = mysqli_query($mysqli,"SELECT `zen_system_id` FROM `oc_wholesale_link` WHERE `model_id`='" . $model . "'");
$id_array = mysqli_fetch_array($result_2);
echo $zen_id = $id_array['zen_system_id'];
}
else
{
echo 'Product Not Found';
}
我已更改此行$model = $model_array['model'] . "</br>";
我只是假设<br>
用于调试目的。
UPDATE
由于您只需要获取oc_product_option_value
中不存在的产品ID,您可以将查询更改为此
$result = mysqli_query($mysqli,"SELECT `product_id` FROM `oc_product_option_value` WHERE `product_id`= '".$product_id."'");
if($rows = mysqli_fetch_array($result))
{
$result = mysqli_query($mysqli,"SELECT `model` FROM `oc_product` WHERE `product_id`= '".$product_id."'");
$model_array = mysqli_fetch_array($result);
$model = $model_array['model'];
//echo $model;
$result_2 = mysqli_query($mysqli,"SELECT `zen_system_id` FROM `oc_wholesale_link` WHERE `model_id`='" . $model . "'");
$id_array = mysqli_fetch_array($result_2);
echo $zen_id = $id_array['zen_system_id'];
}
else
{
echo $product_id . '<br>';
}
答案 1 :(得分:0)
避免此类失败的简单示例:
/* Select queries return a resultset */
$result = mysqli_query($link, "SELECT Name FROM City LIMIT 10")
if (!is_null($result)) {
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* free result set */
mysqli_free_result($result);
} else {
echo "Mysqli query failed: " . mysqli_error();
}