Pagina 1 din 1

Cannot use object of type mysqli_result as array

Scris: Lun Dec 07, 2020
de evident
Salut, m-am lovit de aceasta eroare in scriptul meu...
"Cannot use object of type mysqli_result as array"

Cod: Selectaţi tot

<?php
include"header.php";
require_once("include/functii.php");

$perPage = 10;

$page = 1;
if(!empty($_GET["page"])) {
$page = $_GET["page"];
}

$start = ($page-1)*$perPage;
if($start < 0) $start = 0;

$mydb->setQuery("SELECT * from php_interview_questions limit $start,$perPage");
$faq = $mydb->executeQuery();
$nr = $mydb->num_rows($faq);

if(empty($_GET["rowcount"])) {
$_GET["rowcount"] = $nr;
}
$pages  = ceil($_GET["rowcount"]/$perPage);
$output = '';
if(!empty($faq)) {
$output .= '<input type="hidden" class="pagenum" value="' . $page . '" /><input type="hidden" class="total-page" value="' . $pages . '" />';
foreach($faq as $k=>$v) {
 $output .=  '<div class="question">' . $faq[$k]["question"] . '</div>';
 $output .= '<div class="answer">' . $faq[$k]["answer"] . '</div>';
}

print $output;

}
?>
in linile

Cod: Selectaţi tot

$output .=  '<div class="question">' . $faq[$k]["question"] . '</div>';
 $output .= '<div class="answer">' . $faq[$k]["answer"] . '</div>';

Cannot use object of type mysqli_result as array

Scris: Mar Dec 08, 2020
de MarPlo
Salut,
Din enuntul mesajului de eroare sa intelege ca variabila $faq contine un obiect, nu un array.
Incearca asa:

Cod: Selectaţi tot

foreach($faq as $k=>$v) {
 $output .=  '<div class="question">' . $v->question .'</div>';
 $output .= '<div class="answer">' . $v->answer .'</div>';
}
Daca nu functioneaza, vezi ce structura de date contine $faq ca sa-ti dai seama cum sa preiei datele din ea, cu:

Cod: Selectaţi tot

var_dump($faq);
Ar fi bine sa verifici si functia executeQuery() ca sa vezi cum face comanda la mysql si ce date returneaza.

Re: Cannot use object of type mysqli_result as array

Scris: Joi Dec 10, 2020
de evident

Cod: Selectaţi tot

object(mysqli_result)[5]
  public 'current_field' => int 0
  public 'field_count' => int 3
  public 'lengths' => null
  public 'num_rows' => int 1
  public 'type' => int 0
Asta imi returneaza...cu var_dump();

si eroarea in cele doua lini cu :
Notice: Trying to get property of non-object in
Aste sunt functiile:

Cod: Selectaţi tot

function setQuery($sql='') {
		$this->sql_string=$sql;
	}
	
	function executeQuery() {
		$result = mysqli_query($this->conn, $this->sql_string);
		$this->confirm_query($result);
		return $result;
	}

Cannot use object of type mysqli_result as array

Scris: Vin Dec 11, 2020
de MarPlo
Incearca asa:
Inlocuieste urmatoarele linii din codul tau:

Cod: Selectaţi tot

foreach($faq as $k=>$v) {
 $output .=  '<div class="question">' . $faq[$k]["question"] . '</div>';
 $output .= '<div class="answer">' . $faq[$k]["answer"] . '</div>';
}
Cu acestea:

Cod: Selectaţi tot

while($row = $faq->fetch_assoc()){
  $output .=  '<div class="question">' . $row["question"] . '</div>';
  $output .= '<div class="answer">' . $row["answer"] . '</div>';
}

Re: Cannot use object of type mysqli_result as array

Scris: Vin Dec 11, 2020
de evident
Merge, imi poti explica te rog cum ti-ai dat seama ce trebuie facut si ce era gresit?
Multumesc mult!

Cannot use object of type mysqli_result as array

Scris: Sâm Dec 12, 2020
de MarPlo
Am vazut ca metoda executeQuery() nu transforma in vreun fel datele de la mysqli (cum am presupus ca le returneaza ca obiect) ci le returneaza direct asa cum sunt primite.
Iar datele de la mysqli in forma originala se parcurg pe randuri cu while() in timp ce sunt transformate in obiect ( cu fetch_object() ) sau in array asociativ (cu fetch_assoc() ) cum e acum in codul tau.