0

I have a problem with a MySQL query using a LIMIT and my PHP script. I get the following error: Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in D:\wamp\www\multi screen\company.php

Here is my code:

 $result=mysql_query("select * from safeway LIMIT $start,$end");
  while($o=  mysql_fetch_object($result)){
      echo '<tr style="background-color:white;">';
      echo '<td><input style="width:32;" type="checkbox" name="Edit" value="'.$o->id_pr.'"></td>';
      echo '<td >'.$o->id_pr.'</td>';
      echo '<td>'.$o->name_prod.'</td>';
      echo '<td>'.$o->P_PRICE.'</td>';
      echo '<td>'.$o->V_CODE.'</td>';
      echo '</tr>';
  }

Why am I experiencing this error?

Anil Natha
  • 2,538
  • 6
  • 32
  • 41

1 Answers1

0

Okay, without looking at your code, I can deduce an issue in this line:

$result=mysql_query("select * from safeway LIMIT $start,$end");

So, is $start and $end actually set with a value? If not, you might end up with a query that becomes:

select * from safeway LIMIT ,

Which would never work & would rightfully be parsed as an error. Instead I would recommend validating the values & then build the limit like this:

if (!empty(intval($start) && !empty(intval($end)) {
  $limit = ' LIMIT ' . $start . ',' . $end;
}
$query = "select * from safeway" . $limit;
$result=mysql_query($query);

I say this is a recommendation because I do not see the rest of your logic so unclear how $start and $end are set. But the general concept holds. Just validate that $start and $end have values and then build the LIMIT based on that.

Giacomo1968
  • 24,837
  • 11
  • 67
  • 96