Shane Larson
Shane Larson

Reputation: 597

MySQLi Fetch Object

Is it ok if you do

$TEST = $LINK->query("SELECT * FROM `users`");
while($ROW = $TEST->fetch_object() && $MAX_ROWS < 16){
  $MAX_ROWS++;
  //more code
}

I know you can just add Limit 0,16 to the end of the query, but I was wondering if this syntax works because it seemed to fail.

Sorry I'm just trying to learn as much as I can.

Upvotes: 0

Views: 958

Answers (2)

Berry Langerak
Berry Langerak

Reputation: 18859

I know you can just add Limit 0,16 to the end of the query, but I was wondering if this syntax works because it seemed to fail.

That should just work. Did it fail with an error?

<?php
$max_rows = 0;
$test = true;
while( $test && $max_rows < 16 ) {
   $max_rows++;
   // rest.
}

That works for me and should result in the same?

EDIT: Tobiask is right. If you use "$row = mysqli_fetch_object( $result ) && $max_rows < 16 )" the content of $row will be true, as both tests are successful.

Upvotes: 0

Tobias
Tobias

Reputation: 7380

try this (some brackets added):

$TEST = $LINK->query("SELECT * FROM `users`");
while(($ROW = $TEST->fetch_object()) && $MAX_ROWS < 16){
  $MAX_ROWS++;
  //more code
}

Upvotes: 3

Related Questions