php错误:在布尔值上调用成员函数fetch_assoc()

时间:2018-08-05 12:20:30

标签: php mysql mysqli

我编写了一个非常简单的php脚本,目标是连接到mysql数据库,检索数据并回显数据。脚本如下:

<?php
// Include config file
require_once 'config.php';

// Prepare a select statement
$get_query = "select * from dishes";

$stmt = $mysqli->prepare($get_query);
$stmt->execute();
$stmt->store_result();
$result = $stmt->get_result();

//create array to put the table in
$response = array();
$response['dishes'] = array();

while($dishes = $result->fetch_assoc()){
    //create a temporary array
    $temp = array();

    //inserting the dishes in the temporary array
    $temp['unique_id'] = $dishes['unique_id'];
    $temp['title']=$dishes['title'];

    //push the temporary array inside response
    array_push($response['dishes'],$temp);
}

echo json_encode($response);

?>

在配置文件中,连接到我的数据库是必需的。餐桌很简单,它有2列,一列用于unique_id,一列带有标题。该脚本不言自明,因为我在其中补充了评论。但是,如果运行它,则会出现以下错误:

  

PHP致命错误:未被捕获的错误:在...中对布尔值调用成员函数fetch_assoc()

点指向此文件的位置。我不明白为什么会这样。谢谢!

2 个答案:

答案 0 :(得分:0)

删除

private static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View itemView) {
            super(itemView);
            assignViews();
        }

        private View findViewById(final int id) {
            return itemView.findViewById(id);
        }

        /**
         * viewHolder position
         */
        private int position;
        /**
         * download id
         */
        private int id;

        public void update(final int id, final int position) {
            this.id = id;
            this.position = position;
        }


        public void updateDownloaded() {
            dlItmProgressBar.setMax(1);
            dlItmProgressBar.setProgress(1);

            // Status: Download Complete
            dlItmStatus.setText(R.string.dlmng_status_completed);
            downloadBtn.setVisibility(View.GONE);
            pauseBtn.setVisibility(View.GONE);
            viewBtn.setVisibility(View.VISIBLE);
        }

        public void updateNotDownloaded(final int status, final long sofar, final long total) {
            if (sofar > 0 && total > 0) {
                final float percent = sofar
                        / (float) total;
                dlItmProgressBar.setMax(100);
                dlItmProgressBar.setProgress((int) (percent * 100));
            } else {
                dlItmProgressBar.setMax(1);
                dlItmProgressBar.setProgress(0);
            }

            switch (status) {
                case FileDownloadStatus.error:
                    dlItmStatus.setText(R.string.dlmng_status_error);
                    break;
                case FileDownloadStatus.paused:
                    dlItmStatus.setText(R.string.dlmng_status_paused);
                    break;
                default:
                    dlItmStatus.setText(R.string.dlmng_status_not_downloaded);
                    break;
            }
            // Status Not Downloaded
            downloadBtn.setVisibility(View.VISIBLE);
            pauseBtn.setVisibility(View.GONE);
            viewBtn.setVisibility(View.GONE);
        }

        public void updateDownloading(final int status, final long sofar, final long total, final String speed) {
            final float percent = sofar
                    / (float) total;
            dlItmProgressBar.setMax(100);
            dlItmProgressBar.setProgress((int) (percent * 100));

            switch (status) {
                case FileDownloadStatus.pending:
                    dlItmStatus.setText(R.string.dlmng_status_pending);
                    break;
                case FileDownloadStatus.started:
                    dlItmStatus.setText(R.string.dlmng_status_started);
                    break;
                case FileDownloadStatus.connected:
                    dlItmStatus.setText(R.string.dlmng_status_connected);
                    break;
                case FileDownloadStatus.progress:
                    dlItmStatus.setText(String.format(MyApplication.getInstance().getContext().getResources().getString(R.string.dlmng_status_downloading),(int) (percent*100))); // + "at " + String.valueOf(Integer.parseInt(speed)/1024)+"MB/s");
                    break;
                default:
                    dlItmStatus.setText(MyApplication.getInstance().getContext().getString(
                            R.string.dlmng_status_downloading, status));
                    break;
            }

            // Status Downloading
            downloadBtn.setVisibility(View.GONE);
            pauseBtn.setVisibility(View.VISIBLE);
            viewBtn.setVisibility(View.GONE);
        }

        private TextView dlItmName;
        private TextView dlItmStatus, dlItmSize, dlItmSpeed;
        private ProgressBar dlItmProgressBar;
        private FancyButton downloadBtn, deleteBtn, pauseBtn, viewBtn;

        private void assignViews() {
            dlItmName = (TextView) findViewById(R.id.dlmng_download_Name);
            dlItmStatus = (TextView) findViewById(R.id.dlmng_download_status);
            dlItmSize = (TextView) findViewById(R.id.dlmng_download_size);
            dlItmSpeed = (TextView) findViewById(R.id.dlmng_download_speed);
            dlItmProgressBar = (ProgressBar) findViewById(R.id.dlmng_progress_bar);
            downloadBtn = (FancyButton) findViewById(R.id.dlmng_download_btn);
            deleteBtn = (FancyButton) findViewById(R.id.dlmng_delete_btn);
            pauseBtn = (FancyButton) findViewById(R.id.dlmng_pause_btn);
            viewBtn = (FancyButton) findViewById(R.id.dlmng_view_btn);
        }

    }

    private static class Adapter extends RecyclerView.Adapter<ViewHolder> {

        private FileDownloadListener taskDownloadListener = new FileDownloadSampleListener() {

            private ViewHolder checkCurrentHolder(final BaseDownloadTask task) {
                final ViewHolder tag = (ViewHolder) task.getTag();
                if (tag.id != task.getId()) {
                    return null;
                }

                return tag;
            }

            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                super.pending(task, soFarBytes, totalBytes);
                final ViewHolder tag = checkCurrentHolder(task);
                if (tag == null) {
                    return;
                }

                tag.updateDownloading(FileDownloadStatus.pending, soFarBytes
                        , totalBytes, String.valueOf(task.getSpeed()));
                tag.dlItmStatus.setText(R.string.dlmng_status_pending);
            }

            @Override
            protected void started(BaseDownloadTask task) {
                super.started(task);
                final ViewHolder tag = checkCurrentHolder(task);
                if (tag == null) {
                    return;
                }

                tag.dlItmStatus.setText(R.string.dlmng_status_started);
            }

            @Override
            protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
                super.connected(task, etag, isContinue, soFarBytes, totalBytes);
                final ViewHolder tag = checkCurrentHolder(task);
                if (tag == null) {
                    return;
                }

                tag.updateDownloading(FileDownloadStatus.connected, soFarBytes
                        , totalBytes, String.valueOf(task.getSpeed()));
                tag.dlItmStatus.setText(R.string.dlmng_status_connected);
            }

            @Override
            protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                super.progress(task, soFarBytes, totalBytes);
                final ViewHolder tag = checkCurrentHolder(task);
                if (tag == null) {
                    return;
                }

                tag.updateDownloading(FileDownloadStatus.progress, soFarBytes
                        , totalBytes, String.valueOf(task.getSpeed()));
            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {
                super.error(task, e);
                final ViewHolder tag = checkCurrentHolder(task);
                if (tag == null) {
                    return;
                }

                tag.updateNotDownloaded(FileDownloadStatus.error, task.getLargeFileSoFarBytes()
                        , task.getLargeFileTotalBytes());
                DownloadManager.getImpl().removeTaskForViewHolder(task.getId());
            }

            @Override
            protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                super.paused(task, soFarBytes, totalBytes);
                final ViewHolder tag = checkCurrentHolder(task);
                if (tag == null) {
                    return;
                }

                tag.updateNotDownloaded(FileDownloadStatus.paused, soFarBytes, totalBytes);
                tag.dlItmStatus.setText(R.string.dlmng_status_paused);
                DownloadManager.getImpl().removeTaskForViewHolder(task.getId());
            }

            @Override
            protected void completed(BaseDownloadTask task) {
                super.completed(task);
                final ViewHolder tag = checkCurrentHolder(task);
                if (tag == null) {
                    return;
                }

                tag.updateDownloaded();
                DownloadManager.getImpl().removeTaskForViewHolder(task.getId());
            }
        };

        private View.OnClickListener playButtonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() == null) {
                    return;
                }
                ViewHolder holder = (ViewHolder) v.getTag();
                // to start
                final DownloadManagerModel model = DownloadManager.getImpl().get(holder.position);
                final BaseDownloadTask task = FileDownloader.getImpl().create(model.getUrl())
                        .setPath(model.getPath())
                        .setCallbackProgressTimes(100)
                        .setListener(taskDownloadListener);

                DownloadManager.getImpl()
                        .addTaskForViewHolder(task);

                DownloadManager.getImpl()
                        .updateViewHolder(holder.id, holder);

                task.start();
            }
        };
        private View.OnClickListener pauseButtonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() == null) {
                    Log.i(TAG, "pauseButtonClickListener: tag not present");
                    return;
                }
                Log.i(TAG, "pauseButtonClickListener: tag present");
                ViewHolder holder = (ViewHolder) v.getTag();
                // to pause
                FileDownloader.getImpl().pause(holder.id);
            }
        };
        private View.OnClickListener viewButtonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() == null) {
                    Log.i(TAG, "viewButtonClickListener: tag not present");
                    return;
                }
                Log.i(TAG, "viewButtonClickListener: tag present");
                ViewHolder holder = (ViewHolder) v.getTag();
                // to play video
                Log.i(TAG, "viewButtonClickListener: no player for playing video");
            }
        };
        private View.OnClickListener deleteButtonClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() == null) {
                    Log.i(TAG, "deleteButtonClickListener: tag not present");
                    return;
                }
                Log.i(TAG, "deleteButtonClickListener: tag present");
                final ViewHolder holder = (ViewHolder) v.getTag();
                // to delete
                if (doubleBackToDelete) {
                    new File(DownloadManager.getImpl().get(holder.position).getPath()).delete();
                    //holder.downloadBtn.setEnabled(true);
                    holder.updateNotDownloaded(FileDownloadStatus.INVALID_STATUS, 0, 0);
                }
                doubleBackToDelete = true;
                Toast.makeText(MyApplication.getInstance().getContext(), "Please click again to DELETE", Toast.LENGTH_SHORT).show();
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        doubleBackToDelete=false;
                    }
                }, 2000);
            }
        };

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            ViewHolder holder = new ViewHolder(
                    LayoutInflater.from(
                            parent.getContext())
                            .inflate(R.layout.cell_download_manager, parent, false));

            holder.downloadBtn.setOnClickListener(playButtonClickListener);
            holder.pauseBtn.setOnClickListener(pauseButtonClickListener);
            holder.viewBtn.setOnClickListener(viewButtonClickListener);
            holder.deleteBtn.setOnClickListener(deleteButtonClickListener);
            return holder;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            final DownloadManagerModel model = DownloadManager.getImpl().get(position);

            holder.update(model.getId(), position);
            holder.downloadBtn.setTag(holder);
            holder.deleteBtn.setTag(holder);
            holder.pauseBtn.setTag(holder);
            holder.viewBtn.setTag(holder);
            holder.dlItmName.setText(model.getName());

            DownloadManager.getImpl()
                    .updateViewHolder(holder.id, holder);

            //holder.downloadBtn.setEnabled(true);


            if (DownloadManager.getImpl().isReady()) {
                final int status = DownloadManager.getImpl().getStatus(model.getId(), model.getPath());
                if (status == FileDownloadStatus.pending || status == FileDownloadStatus.started ||
                        status == FileDownloadStatus.connected) {
                    // start task, but file not created yet
                    holder.updateDownloading(status, DownloadManager.getImpl().getSoFar(model.getId())
                            , DownloadManager.getImpl().getTotal(model.getId()), "");
                } else if (!new File(model.getPath()).exists() &&
                        !new File(FileDownloadUtils.getTempPath(model.getPath())).exists()) {
                    // not exist file
                    holder.updateNotDownloaded(status, 0, 0);
                } else if (DownloadManager.getImpl().isDownloaded(status)) {
                    // already downloaded and exist
                    holder.updateDownloaded();
                } else if (status == FileDownloadStatus.progress) {
                    // downloading
                    holder.updateDownloading(status, DownloadManager.getImpl().getSoFar(model.getId())
                            , DownloadManager.getImpl().getTotal(model.getId()), "");
                } else {
                    // not start
                    holder.updateNotDownloaded(status, DownloadManager.getImpl().getSoFar(model.getId())
                            , DownloadManager.getImpl().getTotal(model.getId()));
                }
            } else {
                holder.dlItmStatus.setText(R.string.dlmng_status_loading);
                //holder.downloadBtn.setEnabled(false);
            }
        }

        @Override
        public int getItemCount() {
            return DownloadManager.getImpl().getTaskCounts();
        }
    }

答案 1 :(得分:0)

当您可以使用fetch_all()时为什么要使用While循环。

$stmt = $this->conn->prepare("select * from dishes");
if ($stmt->execute()) {
        $dishes = $stmt->get_result()->fetch_all();
        $stmt->close();
}