0

I have the following query and i want to run the whole table in order to give me one-by-one each value of the table. I need that to use the specific row value in another query.

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>";
    }

What i get is "Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given"

It seems i get a boolean(false) as a result, the query works fine if a place a number instead of a variable.

Any help on this please?

Further 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)");

what i tried but didn't worked yet:

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

It needs to be like this '$variable' somehow because it returns nothing as i can see.

user3127632
  • 377
  • 1
  • 5
  • 20
  • It's a good idea to check the return value of `mysqli_query` (and indeed most mysqli_ functions) before using it, in case there is an error. For example: `if ($result) { // process data } else { // handle error }` – pwaring May 24 '14 at 18:40
  • As per [the documentation](http://us1.php.net/mysqli_query), `mysqli_query` returns `false` if the query fails. So you should have an `if` clause like the one pwaring suggested. – NathanW May 24 '14 at 18:42

1 Answers1

0

Remove the single quote around the offset.

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

You should also be checking for errors:

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));
}
mseifert
  • 5,095
  • 9
  • 35
  • 91
  • and semicolon after offset it's useless – turson May 24 '14 at 18:39
  • True enough, but that isn't producing the error. Thanks, I will remove it. – mseifert May 24 '14 at 18:40
  • Sure, but it's useless as I wrote and can execute some errors in wrong usage – turson May 24 '14 at 18:41
  • lol thanks! in addition if you can help me further more,i need to use the output of this in this query (replace the 0100100 with the above query result - see original post update), i tried to do it like the way you showed me in this but i made a mistake with quotes i guess – user3127632 May 24 '14 at 18:51
  • It would be best to mark this answer correct, if it was, and then post a new question. However, to answer your new question, the format for `INSERT...SELECT` should be `INSERT INTO table (col1, col2, ..., coln) SELECT col1, col2, ..., coln FROM table2 WHERE condition`. – mseifert May 24 '14 at 19:09