-2

lets see code

public function chekfactor( $factor,$user) {    
               $arrfactor=preg_split('/<td>/',$factor);
                  $arrusers=preg_split('/<td>/',$user);
             $tedstore=count($arrusers) ;
              $tedkala=count($arrfactor) ;
               $inttedstore=(int) $tedstore;
 $inttedkala=(int)$tedkala;
$afa="0";
$a=(int)$afa;
 for (;$a<$inttedstore;){
  $storeusername=$arrusers[$a];
      $faktorss=$arrfactor[$a];
  $tablename='devlist'.$storeusername;
  echo   $tablename;
      echo   $faktorss;
           $result = $this->conn->query("SELECT id FROM'".$tablename."' WHERE prcode='".$faktorss."' ");

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                                    $user = array();
                    $user[$faktorss] = $row["id"];


              return $user;
            }
        }else {

             return "no";
        }


  $a++;
}

and give to me the errors

trying to get property of non-object on line 543 means " if ($result->num_rows > 0) { "

why happen and how fix it ?

Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
alis
  • 5
  • 4
  • that means you don't have result in `$result` add `if` condition to check it has value or not – Jigar Shah Oct 03 '17 at 12:02
  • 1
    Your query syntax is off. You have quotes around the tablename, and no space between `FROM` and the tablename – Qirel Oct 03 '17 at 12:08

3 Answers3

2

change the line

if ($result->num_rows > 0)

for

if (!empty($result)) // make sure it is having value then use `num_rows` on it

The issue is that you are trying to access to num_rows property when $result is empty.

Jigar Shah
  • 6,143
  • 2
  • 28
  • 41
Synkronice
  • 179
  • 1
  • 9
  • 2
    It is better to use `isset` or `empty` instead of just an `if`. – teeyo Oct 03 '17 at 12:03
  • 1
    The value will always be set, it will either be an object or `false` - so checking on the variable directly is fine as this code is structured. – Qirel Oct 03 '17 at 12:11
  • I think that always where is possible we should use IF in a positive way, for to improve the reading. To use "if ($result)" is completely valid in a test environment. – Synkronice Oct 03 '17 at 12:33
1

i guess your query is not working because of your quotes and space

"SELECT id FROM'".$tablename."' WHERE prcode='".$faktorss."'";

query will print like

SELECT id FROM'table_1' WHERE..

you can change like

"SELECT id FROM ".$tablename." WHERE prcode=".$faktorss."";
0

You can do

if($result && !empty($result)){
   $num_rows = $result->num_rows;
   if($num_rows > 0){
     //the logic here
   }
}
adkstar
  • 178
  • 1
  • 10
  • it will throw the same error as OP mentioned , problem is $result is empty so you can not access `num_rows` on it – Jigar Shah Oct 03 '17 at 12:08