Yêu cầu thg 4 20, 2020 4:30 SA 125 0 2
  • 125 0 2
0

Hỏi về việc sử dụng file PHP load dữ liệu từ database thành json

Chia sẻ
  • 125 0 2

Các anh chị có thể giúp em một chút được không ạ. Em thực sự không biết j về PHP em đang code một project android và theo hướng dẫn trên youtube em đã dùng file PHP để convert dữ liệu qua dạng json rồi để chuyển về java sử dụng cho project. Hôm nay em có tạo một file PHP để load dữ liệu về nhưng gặp lỗi như sau Use of undefined constant mysqli_fetch_assoc - assumed 'mysqli_fetch_assoc' và Illegal string offset em đã tra gg nhưng chưa tìm được câu trả lời mong a chị có thể giúp. https://musicbachchien.000webhostapp.com/Server/chudevatheloaitrongngay.php đây là đường dẫn tới file PHP của em. Còn đây là code file PHP của em mong được anh chị giúp đỡ ạ.

<?php
	require "connect.php";

	class TheLoai{
			function TheLoai($idtheloai,$idkeychude,$tentheloai,$hinhtheloai){
				$this->IdTheLoai =$idtheloai;
				$this->IdKeyChuDe = $idkeychude;	
				$this->TenTheLoai = $tentheloai;
				$this->HinhTheLoai = $hinhtheloai;
	}}
	class ChuDe{
			function ChuDe($idchude,$tenchude,$hinhchude)
	{
			$this->IdChuDe =$idchude;
			$this->TenChuDe =$tenchude;
			$this->HinhChuDe =$hinhchude;
	}
	}


	$arraytheloai =array();
	$arraychude = array();

	$querytheloai = "SELECT DISTINCT * FROM theloai ORDER BY rand(". date("ymd"). ") LIMIT 3";
	$datatheloai = mysqli_query($con,$querytheloai);
	while($row = mysqli_fetch_assoc){
		array_push($arraytheloai, new TheLoai($row['idTheLoai'],$row['idChuDe'],$row['TenTheLoai'],$row['HinhTheLoai']));

	}


	$querychude = "SELECT DISTINCT * FROM chude ORDER BY rand(". date("ymd"). ") LIMIT 3";
	$datachude = mysqli_query($con,$querychude);
	while($row = mysqli_fetch_assoc){
		array_push($arraychude, new ChuDe($row['idChuDe'],$row['TenChuDe'],$row['HinhChuDe']));

	}
	
	$arraychudetheloai = array('TheLoai'=>$arraytheloai,'ChuDe'=>$arraychude);
	echo json_encode($arraychudetheloai);


?>

2 CÂU TRẢ LỜI


Đã trả lời thg 4 20, 2020 4:47 SA
Đã được chấp nhận
0

bạn thử chuyển như này nhé:

  • chuyển while($row = mysqli_fetch_assoc){ sang while($row = mysqli_fetch_assoc($datatheloai)){
  • chuyển while($row = mysqli_fetch_assoc){ sang while($row = mysqli_fetch_assoc($datachude)){
Chia sẻ
thg 4 20, 2020 4:53 SA

Dạ em cảm ơn ạ. Có gì mong được các anh chị giúp đỡ tiếp ạ

Avatar Nguyễn Văn Bách @bachnguyen2907
thg 4 20, 2020 4:56 SA

@chientrang26 function TheLoai trong class TheLoai đổi thành function __construct(), tương tự với ChuDe

Đã trả lời thg 4 20, 2020 4:36 SA
0

bạn format code đi nhìn cho dễ, khó đọc quá, kiểu như thế này

class ClassA {}

Screenshot from 2020-04-20 11-37-26.png

Chia sẻ
Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí