0

I am trying to find the last teacher id from my database and getting this error. Fatal error: Cannot use object of type mysqli_result as array in .My Codes are as follows. Any Help ?

class tinyteacher {
public function getLastTeacherId(){
  $query = ("SELECT `teacherid` FROM `teacher` ORDER BY `teacherid` DESC ");
  $result = $this->db->select($query);
  return $result;
 }
}

<div class="col-sm-6">
  <input type="text" name="teacherid" value="
  <?php 
    $teacher = new tinyteacher();
    $lastteacher = $teacher->getLastTeacherId();
   echo $lastteacher;   
  ?> 
</div>
Ami Hasan
  • 125
  • 14

2 Answers2

0

your function return $result; array object

and you print array object in echo echo use for print string not for array

so use print_r instead of echo

like print_r($lastteacher);

Bhargav Chudasama
  • 6,928
  • 5
  • 21
  • 39
0

Can't use object of type mysqli_result as array

Use mysqli_fetch_assoc or mysqli_fetch_array to fetch a result row as an associative array.

$query = "SELECT 1";
$result = $mysqli->query($query);
$data_array = $result->fetch_assoc()

and for more reference click here

A.D.
  • 2,352
  • 2
  • 15
  • 25
  • you copy from here:https://stackoverflow.com/questions/16525413/fatal-error-cannot-use-object-of-type-mysqli-result – Bhargav Chudasama Nov 06 '17 at 06:37
  • write your own answer not copy another user answer – Bhargav Chudasama Nov 06 '17 at 06:38
  • I think this site for help not for the allegations. And yes I copied but not say this is my code. I share because he needed, thanks for your folks up @Bhargav. – A.D. Nov 06 '17 at 06:41
  • but you paste refrence also or link of this answer in comment boy wake up – Bhargav Chudasama Nov 06 '17 at 06:42
  • `$mysqli = mysqli_connect("host","user","password","your_db"); $query = "SELECT 1"; $result = $mysqli->query($query); $data_array = $result->fetch_assoc()` use your parameters for connection. – A.D. Nov 06 '17 at 06:49