This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Object-Oriented PHP Forum

View Course » View Exercise

0 points
79a5a54186a1bd2c8ad31f8054d0aea8?s=140&d=retro
Submitted by
yankeebass
about 2 years ago

num_rows = 1 but no data?

Am i missing something?
I'm getting 1 record for num_rows but no data...?

Nr. of records: 1

// test query
$sql = "SELECT cmp_name, cmp_fcity FROM cicmpy WHERE cmp_name LIKE '%SSS%' ORDER BY cmp_name";
$result = $db->query($sql);

// current records
$currRecord = $result->count();
echo "Nr. of records: " . $currRecord . "<br>";

// fetch data
while( $row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_BOTH) ) {
    // associated
      echo "A " . $row['cmp_name'].", ".$row['cmp_fcity']."<br />";
    // numeric
      echo "B " . $row[5].", ".$row[10]."<br />";
}

class db {
    public function query($query) {           
        return new resultset($query,$this->connection,$this->debug);
    }
}

class resultset implements Countable,Iterator {
    public function resultset($query,$link,$debug = false){
        $this->connection = $link;
        $this->debug = $debug;

        $this->result = sqlsrv_query($this->connection, $query, array(),  array('Scrollable' => SQLSRV_CURSOR_STATIC));
        if ($this->result == false){
            $this->debug('Query Failed: '.$query.' '.$this->errors(),true);
            return false;
        } else {
            return $this->result;
        }
    }
}