1

I do extract a table, the table even has the correct number of rows. HOWEVER, ALL the rows have the same information: the first row, the one that indicates the headers ie: FirstName, Phone, Address, etc.

The code below shows just a bit how I am doing it. I reiterate, the mysqli connection is indeed extracting a table with data, with the correct row count. But I am not getting the correct info.

public function queryexec()
{   
  if(isset($this->query))
  {
    //execute prepared query and store in result variable.
    $this->result = $this->connection->query($this->query);
    echo 'Table Row Count: '. $this->result->num_rows;
        
        echo '<br/>';
        
        //fetch_array gives me table but only headers but correct amount of rows.
        //Fetch_object doesnt work throws me error: 
        //Fatal error: Cannot use object of type stdClass as array   
        while($row = $this->result->fetch_array()){
             
             printf ("%s (%s)\n", $row[0], $row[2][1]);
            //echo 'Table Data: '. $newdata["LastName"];
           //echo '<br/>';
        }
        
        return true;
  }
  return false;
}
0

1 Answer 1

2

You're calling fetch_object() but then you're accessing row elements as if it's an ordinal array.

You should either access a column like a field of an object $row->LastName or else change the way you fetch a row to use $this->result->fetch_array().


I just tested your code, using PHP 5.3.26, and MySQL 5.6.13. I got no errors, and I got distinct data for all three rows:

mysql> show create table test.foo\G

CREATE TABLE `foo` (
  `id` int(11) NOT NULL,
  `z` int(11) DEFAULT NULL,
  `LastName` text,
  `t` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

mysql> insert into foo (id, lastname) values 
(1, 'Granger'), (2, 'Potter'), (3, 'Weasley');

$ php 19236839.php
Table Row Count: 3<br/>
1 (r)
2 (o)
3 (e)

Note that $row[2][1] returns a single character, the second character in the string.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.