简体   繁体   English

无限循环— PHP中的SQL查询

[英]Infinite loop — sql query in php

Basically, I'm trying to make a function to take entries in a database by gender, and put it into an array that's used to display a table. 基本上,我试图创建一个函数来按性别在数据库中输入条目,并将其放入用于显示表的数组中。 However, somewhere in the code below, I think I'm running into an infinite loop-- when I call the function, the page loads the right entries, but keeps loading, and the scrollbar gets longer and longer. 但是,在下面的代码中,我认为我正在陷入无限循环-当我调用该函数时,页面会加载正确的条目,但会不断加载,滚动条会越来越长。 I'm very new to php/mysql/web development in general, so any help would be much appreciated! 我一般来说对php / mysql / web开发是非常陌生的,所以任何帮助将不胜感激!

<?
function getbyGender($g, &$form_data){
$sql = "select * from user_info where gender='".$g."'";
echo $sql;
$query = mysqli_query($this->dblink, $sql) or die (mysql_error());


while($info = mysqli_fetch_array($query, MYSQL_ASSOC))
{

  foreach($info as $key=>$value)
  {
      $form_data[$i][$key] = $value;
  }

  $i++;
}
} ?>

Code that displays the table (using the yuitable API): 显示表的代码(使用yuitable API):

//Array to store all column labels to be used
$fields = array(
    'uname'=>'Username',
    'flname'=>'Full Name',
    'bday'=>'Birthday',
    'gender'=>'Gender',
    'comments'=>'Comments',
    'email'=>'Email',
);

$columns4 = $fields;
$t_columns4 = array();

//Format the table
foreach($columns4 as $key=>$value)
{
    $t_columns4[$key]['key'] = $value;
    $t_columns4[$key]['label'] = $value;
    $t_columns4[$key]['sortable'] = "false";
    $t_columns4[$key]['className'] = '';
    if ($value == 'Comments')
    {
        $t_columns4[$key]['width'] = '200';
    }
    else{


      $t_columns4[$key]['width'] = '100';

    }
}



$form_data = array();
$urls4 = array();
$sort4 = array();
$form_table = new yuitable("formtable");
$form_table->setColumns($t_columns4);



$local_mysql->getByGender('f', $form_data);

mysqli_fetch_array($query, MYSQL_ASSOC) should be mysqli_fetch_array($query, MYSQLI_ASSOC) . mysqli_fetch_array($query, MYSQL_ASSOC)应该是mysqli_fetch_array($query, MYSQLI_ASSOC) That should be the source of the problem. 那应该是问题的根源。 The while loop is fine for that, but you probably don't need a while and a foreach loop. 虽然while循环对此很合适,但是您可能不需要一会儿和一个foreach循环。

我实际上并没有定义$ i =0。现在我觉得太愚蠢了……感谢Mark Ba​​ker指出了这一点,以及其他所有人的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM