我的功能有問題。我用這個呼叫 getclass 函式。
<?php
require_once './DbOperations.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_POST['username'])) {
$db = new DbOperations();
$classjson = $db->getclass($_POST['username']);
echo $classjson;
}
}
?>
但它不適合我。這不起作用 [$teacherfullname,$teacherpicture] = $this->getteacher($teacher); 但是當我把它放在頂部并將 $teacher 更改為 $uuid 時它很好。但我需要$老師。
public function getclass($uuid) {
//when i put here its working with $uuid but i need $teacher
[$teacherfullname,$teacherpicture] = $this->getteacher($uuid);
$stmt = $this->con->prepare("SELECT `id`, `name`, `teacher`, `student` FROM `class` WHERE teacher= ? OR student= ?");
$stmt->bind_param("ss", $uuid, $uuid);
$stmt->execute();
$stmt->bind_result($id, $name, $teacher, $student);
$product = array();
while($stmt->fetch()) {
//this is what not working
[$teacherfullname,$teacherpicture] = $this->getteacher($teacher);
$temp = array();
$temp['id'] = $id;
$temp['name'] = $name;
$temp['teacher'] = $teacherfullname;
$temp['picture'] = $teacherpicture;
$temp['student'] = $student;
array_push($product, $temp);
}
return json_encode($product);
}
private function getteacher($uuid) {
$stmt = $this->con->prepare("SELECT id, fullname, picture FROM users WHERE uuid = ?");
$stmt->bind_param("s", $uuid);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$stmt->store_result();
return [$row['fullname'], $row['picture']];
}
請幫助我找到錯誤。
uj5u.com熱心網友回復:
編輯:從 OP 誤讀,對不起。
嘗試更換
$stmt->bind_param("s", $uuid);
和 :
$stmt->bind_param("uuid", $uuid);
不要像這樣分配變數。
[$teacherfullname,$teacherpicture] = $this->getteacher($teacher);
我建議你做這樣的事情:
...
$teacher = $this->getteacher($teacher);
$temp['teacher'] = $teacher['fullname'];
$temp['picture'] = $teacher['picture'];
...
private function getteacher($uuid) {
... //use named indexes, easier to maintain
return ["fullname" => $row['fullname'], "picture" => $row['picture']];
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/421384.html
標籤:
下一篇:如何在函式的某個斷點處退出代碼
