Call to undefined method mysqli_stmt::fetch_assoc()

Status
Dieses Thema wurde gelöst! Zur Lösung gehen…

Loddar1

Erfahrenes Mitglied
Hallo Leute, ich komme nicht dahinter wo ich einen Fehler habe.
Hier mal alter Code:
PHP:
$sql = "select * from table";
$res = mysqli_query($con,$sql);
if (mysqli_num_rows($res) == 0)
 {   
  $sql = "INSERT INTO `table` (`id`, `day_id`, `day_value`, `yesterday_id`, `yesterday_value`, `week_id`, `week_value`, `month_id`, `month_value`, `year_id`, `year_value`, `all_value`, `record_date`, `record_value`) VALUES ('1', '" . date("z") . "',  '1', '" . (date("z")-1) . "', '0', '" . date("W") . "', '1', '" . date("n") . "', '1','".date("Y")."','1','1',NOW(),'1')";
  mysqli_query($con,$sql);

$sql = "select * from table";
  $res = mysqli_query($con,$sql);
 }

$row = mysqli_fetch_assoc($res);

$day_id = $row['day_id'];
$day_value = $row['day_value'];
$yesterday_id = $row['yesterday_id'];
$yesterday_value = $row['yesterday_value'];
$week_id = $row['week_id'];
$week_value = $row['week_value'];
$month_id = $row['month_id'];
$month_value = $row['month_value'];
$year_id = $row['year_id'];
$year_value = $row['year_value'];
$all_value = $row['all_value'];
$record_date = $row['record_date'];
$record_value = $row['record_value'];
Das funktioniert einwandfrei!
Mein statement Code meldet einen Fehler und zwar nur bei einem erstaufruf (nach der Installation) ansonsten nicht!

hier mal der neue Code:
PHP:
$stmt = $con->prepare("select id,day_id,day_value,yesterday_id,yesterday_value,week_id,week_value,month_id,month_value,year_id,year_value,all_value,record_date,record_value from table");
$stmt->execute();
$res = $stmt->get_result();
if ($res->num_rows == 0)
 { 
 $stmt = $con->prepare('INSERT INTO table (day_id,day_value,yesterday_id,yesterday_value,week_id,week_value,month_id,month_value,year_id,year_value,all_value,record_date,record_value,id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
 $id = $con->real_escape_string ('1');
 $day_id = $con->real_escape_string (date("z"));
 $day_value = $con->real_escape_string ('1');
 $yesterday_id = $con->real_escape_string ((date("z")-1));
 $yesterday_value = $con->real_escape_string ('0');
 $week_id = $con->real_escape_string (date("W"));
 $week_value = $con->real_escape_string ('1');
 $month_id = $con->real_escape_string (date("n"));
 $month_value = $con->real_escape_string ('1');
 $year_id = $con->real_escape_string (date("Y"));
 $year_value = $con->real_escape_string ('1');
 $all_value = $con->real_escape_string ('1');
 $record_date = $con->real_escape_string ('NOW()');
 $record_value = $con->real_escape_string ('1');
 $stmt->bind_param('iiiiiiiiiiisii', $day_id,$day_value,$yesterday_id,$yesterday_value,$week_id,$week_value,$month_id,$month_value,$year_id,$year_value,$all_value,$record_date,$record_value,$id);
 $stmt->execute();
 
 $res = $con->prepare("select id,day_id,day_value,yesterday_id,yesterday_value,week_id,week_value,month_id,month_value,year_id,year_value,all_value,record_date,record_value from table");
 $res->execute();
 }
 
$row = $res->fetch_assoc(); // FEHLER

$day_id = $row['day_id'];
$day_value = $row['day_value'];
$yesterday_id = $row['yesterday_id'];
$yesterday_value = $row['yesterday_value'];
$week_id = $row['week_id'];
$week_value = $row['week_value'];
$month_id = $row['month_id'];
$month_value = $row['month_value'];
$year_id = $row['year_id'];
$year_value = $row['year_value'];
$all_value = $row['all_value'];
$record_date = $row['record_date'];
$record_value = $row['record_value'];

Die Fehlermeldung: Fatal error: Uncaught Error: Call to undefined method mysqli_stmt::fetch_assoc()

Habe schon gesucht und versucht, aber ich habe nichts anderes gefunden.
Sieht einer meinen Dummen Fehler?
Besten Dank im vorraus Loddar1

Achso, das ganze läuft auf einem XAMPP mit php 8.1.6
 
einmal ist $res das Resultat eines Querys (Zeile 3), dann wird es ein Query (Zeile24)?!?!?!
Müsste Zeile 24 bis 28 nicht so lauten
PHP:
$stmt = $con->prepare("select id,day_id,day_value,yesterday_id,yesterday_value,week_id,week_value,month_id,month_value,year_id,year_value,all_value,record_date,record_value from table");
$stmt->execute();
 }
 
$res = $stmt->fetch_assoc(); //Kein Fehler mehr?
EDIT: Yo, Yaslaw hats schon angesprochen.
Habs gerade nachgeschaut.
Du brauchst ein mysqli_result zurück
PHP: mysqli::execute_query - Manual
welches wiederum die fetch_assoc-Methode hat
PHP: mysqli_result - Manual
Müsste dann so sein:
PHP:
$stmt = $con->prepare("select id,day_id,day_value,yesterday_id,yesterday_value,week_id,week_value,month_id,month_value,year_id,year_value,all_value,record_date,record_value from table");
$stmt->execute();
 }
$res =  $stmt->Get_result();
$einArray = $res->fetch_assoc(); //Kein Fehler mehr?
 
Zuletzt bearbeitet:
Toll Zvoni, habe immer falsch gedacht!!!
Aussehen muß es aber so:
PHP:
$stmt = $con->prepare("select id,day_id,day_value,yesterday_id,yesterday_value,week_id,week_value,month_id,month_value,year_id,year_value,all_value,record_date,record_value from loccounterv");
$stmt->execute();
$res =  $stmt->Get_result();
}
$row = $res->fetch_assoc();

und jetzt habe ich keine Fehlermeldung mehr.

Danke nochmal an Yaslaw und vor allen an Zvoni, der es für mich gut verständlich gemacht hat.

Gruß Loddar1
 
Toll Zvoni, habe immer falsch gedacht!!!
Aussehen muß es aber so:
PHP:
$stmt = $con->prepare("select id,day_id,day_value,yesterday_id,yesterday_value,week_id,week_value,month_id,month_value,year_id,year_value,all_value,record_date,record_value from loccounterv");
$stmt->execute();
$res =  $stmt->Get_result();
}
$row = $res->fetch_assoc();

und jetzt habe ich keine Fehlermeldung mehr.

Danke nochmal an Yaslaw und vor allen an Zvoni, der es für mich gut verständlich gemacht hat.

Gruß Loddar1
Eh? War ich ja nah dran.
Und das obwohl ich keine Ahnung von PHP habe (davon aber jede Menge!)
;)
 
Status
Dieses Thema wurde gelöst! Zur Lösung gehen…
Zurück