动态更改php sql语句中的Offset

时间:2014-05-24 18:32:02

标签: php mysql sql limit offset

我有以下查询,我想运行整个表,以便逐一给出每个表的值。我需要在另一个查询中使用特定的行值。

for ($qoffset=0; $qoffset<=5; $qoffset++) {
$result = mysqli_query($con,"select nf865_virtuemart_products.product_sku  from nf865_virtuemart_products order by product_sku asc limit 1 offset '".$qoffset."';");
  echo "offset='$qoffset' <br>";
    while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
        echo $row[0];
        echo "<br>";
    }

我得到的是“警告:mysqli_fetch_array()期望参数1是mysqli_result,布尔给定”

结果似乎我得到一个布尔值(false),如果放置数字而不是变量,查询工作正常。

对此有任何帮助吗?

进一步的sql:

mysqli_query($con,"Insert  Ignore Into nf865_virtuemart_product_medias(virtuemart_product_id,virtuemart_media_id,ordering) VALUES (
(Select nf865_virtuemart_products.virtuemart_product_id from nf865_virtuemart_products where nf865_virtuemart_products.product_sku LIKE '0100100'),
(Select nf865_virtuemart_medias.virtuemart_media_id from nf865_virtuemart_medias 
where  nf865_virtuemart_medias.file_url LIKE '%0100100 b.jpg' or nf865_virtuemart_medias.file_url LIKE %0100100 B.jpg'),2)");

我尝试但尚未奏效的事情:

$query1 = mysqli_query($con,"Select nf865_virtuemart_products.virtuemart_product_id from nf865_virtuemart_products where nf865_virtuemart_products.product_sku LIKE '".$product_id."'");

它必须像某个'$ variable'一样,因为它看不到任何东西。

1 个答案:

答案 0 :(得分:0)

删除偏移附近的单引号。

$result = mysqli_query($con,"select nf865_virtuemart_products.product_sku  from nf865_virtuemart_products order by product_sku asc limit 1 offset ".$qoffset);

您还应该检查错误:

if ($result == false){
    // you should have some logging method to log this error so you can check on it and return false to the calling function. I am using `die` here so you can at least see the error
    die("Sql Error: " . mysqli_error($con));
}