S
S
Sieghardt Meisner2016-01-23 14:37:01
PHP
Sieghardt Meisner, 2016-01-23 14:37:01

Why after mysqli:fetch it is not possible to use another mysqli:fetch, otherwise an error?

I'm trying to nest one mysqli query into another (no other way). After the first request, I start executing the second one and get an error:

Fatal error: Call to a member function fetch_assoc() on a non-object in C:\Sites\home\jarga-land.ru\www\backend\admin_product_AE.php on line 45

Piece of code:
$results = $mysqli->prepare("SELECT category.name_category, product.*  FROM `category` INNER JOIN `product` ON id_cat = category_id AND product.id = $prod_id");
$results->execute(); //Execute prepared Query
$results->bind_result($cat_name, $id, $title, $description, $full_description, $price, $image, $category_id);
$results->fetch();

//bla bla bla html code

<?php
  //for($sg=0;$sg<5;$sg++){ 
  $q = $mysqli->query("SELECT * FROM `category`");
  while($all_cat=$q->fetch_assoc())   // Call to a member function fetch_assoc() on a non-object in ...
  {
  $cur_cat_name=$all_cat['name_category'];
    //echo $id_cat;
  if($all_cat['id_cat']!=$category_id)
  {
    echo '<option value="'.$all_cat['id_cat'].'">'.$cur_cat_name.'</option>';
  }
  else echo '<option selected value="'.$all_cat['id_cat'].'">'.$cur_cat_name.'</option>';
  }
  ?>

I cannot understand in any way why at performance of the second request the variable is considered NOT object. I thought the problem was in the query to the database, I tried others (even copied the first one) all the same to none. What's the matter then?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shell_execute, 2016-01-24
@shell_execute

You should think about writing code more competently.
Your $mysqli->query returns FALSE and you don't handle this situation in any way - php.net/manual/ru/mysqli.query.php . Read the documentation and look at the examples, there is a solution to the problem like you have.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question