将 mysql 转换为 mysqli google 图表的程序方式

convert mysql to mysqli procedural way for google charts

我正在尝试将我自己使用的一些 google 图表从 mysql 升级到 mysqli procedural 方式,并且我无法为 google 图表重新创建正确的数组以供理解。我想我离我很近,我正在失去一些东西。欢迎任何帮助。

这是我的旧代码:

mysql_select_db($database_localhost, $localhost);
$query_Recordset1 = "select count(*) as count,visible from packs where type=1 and serial=1 group by visible";
$Recordset1 = mysql_query($query_Recordset1, $localhost) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

    $data[0] = array('visible','count');        
    for ($i=1; $i<($totalRows_Recordset1+1); $i++)
    {
        $data[$i] = array(mysql_result($Recordset1, $i-1,'visible'),
            (int) mysql_result($Recordset1, $i-1, 'count'));
    }   

结果为:

[["visible","count"],["0",266],["1",1466],["2",1],["3",59]]

但是当我升级时(或尝试将代码升级到 mysqli):

$query_Recordset1 = "select count(*) as count,visible from packs where type=1 and serial=1 group by visible";
$Recordset1 = mysqli_query($connection,$query_Recordset1) or die(mysqli_error($mysqli));
$row_Recordset1 = mysqli_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysqli_num_rows($Recordset1);

# set heading 
    $data[0] = array('visible','count');    
    for ($i=1; $i<=($totalRows_Recordset1+1); $i++)
              {
                  mysqli_data_seek($Recordset1, $i-1);
                    $data[$i] = array((string) mysqli_fetch_array($Recordset1)['visible'],(int) mysqli_fetch_array($Recordset1)['count']);

              } 

它的结果是:

[["visible","count"],["0",1466],["1",1],["2",59],["3",0],["",0]]

这显然与我想要的结果不匹配,因为由于某种原因一列被偏移了一列(请参阅根本未获取值 266)

不确定为什么你的代码是这样构造的,你可以通过使用标准循环来简化它....

$query_Recordset1 = "select count(*) as count,visible 
                        from packs 
                        where type=1 and serial=1 
                        group by visible";
$Recordset1 = mysqli_query($connection,$query_Recordset1) or die(mysqli_error($mysqli));

# set heading 
$data = [['visible','count']];    
while ($row_Recordset1 = mysqli_fetch_assoc($Recordset1) )
{
    $data[] =[$row_Recordset1['visible'],(int) $row_Recordset1['count']];
} 

在您的代码中,行...

$data[$i] = array((string) mysqli_fetch_array($Recordset1)['visible'],
         (int) mysqli_fetch_array($Recordset1)['count']);

检索 2 行数据,一行用于 visible 部分,另一行用于 count 部分,这就是数据偏移的原因。