未能值在循环分配到数组

编程入门 行业动态 更新时间:2024-10-23 01:31:28
本文介绍了未能值在循环分配到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个code:

function retrieve_answers($array = array(), $id = null) { include(root_path . '\config.php'); if($id == null) { $id = $this->question_id; } $query = mysqli_query($link, "SELECT * FROM `answers` WHERE `question_id`='$id'"); if(!mysqli_num_rows($query)) { throw new Exception('Question not found.'); } /* - Retrieves the answer rows - Loops through the array - Indexes the array and assigns the answerID to the index */ else { while($result = mysqli_fetch_array($query)) { for($i=0;$i<mysqli_num_rows($query);$i++) { $array[$i] = $result["id"]; } } } }

这是一个类的一部分。

Which is a part of a class.

什么是我想要做什么?我想接受一个数组作为参数,并分配值数组,值其链接到的问题是answerIDs。

What am I trying to do? I am trying to accept an array as a parameter, and assign values to the array, the values are to be answerIDs which are linked to the question.

该文件test.php的是在这里:

The test.php file is here:

<?php define('root_path', realpath(dirname(__FILE__))); include(root_path . '\config.php'); require_once(root_path . '\includes\question.class.php'); $q = new Question(3); $array = array(); $q->retrieve_answers($array); var_dump($array); ?>

会发生什么?当我尝试倾销阵列进行调试,这表明该数组包含什么:

What happens? When I try to debug by dumping the array, it shows that the array contains nothing:

阵列(0){}

我试图执行MySQL的通过类来调试的结果,它确实成功获取答案的ID,所以我pretty正面的问题是阵列中的

I tried to execute the MySQL result through the class to debug, and it does succeed to retrieve the answer IDs, so I'm pretty positive the issue is in the array.

我会高兴地得到帮助,在此先感谢。

I would happy to get assistance, thanks in advance.

推荐答案

在这样的函数返回值

function retrieve_answers($array = array(), $id = null) { include(root_path . '\config.php'); if($id == null) { $id = $this->question_id; } $query = mysqli_query($link, "SELECT * FROM `answers` WHERE `question_id`='$id'"); if(!mysqli_num_rows($query)) { throw new Exception('Question not found.'); } /* - Retrieves the answer rows - Loops through the array - Indexes the array and assigns the answerID to the index */ else { $i = 0; while($result = mysqli_fetch_array($query)) { $array[$i] = $result["id"]; $i++; } return $array; } }

,然后把它作为

$arr = $q->retrieve_answers($array); var_dump($arr);

更多推荐

未能值在循环分配到数组

本文发布于:2023-11-30 01:22:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   分配

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!