猿问

将 mysql 转换为 mysqli 程序方式以用于谷歌图表

我正在尝试将我自己使用的一些谷歌图表从 mysql 升级到 mysqli程序方式,并且我在重新创建正确的数组以供谷歌图表理解时遇到困难。我想我离我很近,我失去了一些东西。欢迎任何帮助。


这是我的旧代码:


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]]

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



慕运维8079593
浏览 75回答 1
1回答

至尊宝的传说

不确定为什么你的代码是这样构造的,你可以通过使用标准循环来简化它......$query_Recordset1 = "select count(*) as count,visible&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from packs&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where type=1 and serial=1&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; group by visible";$Recordset1 = mysqli_query($connection,$query_Recordset1) or die(mysqli_error($mysqli));# set heading&nbsp;$data = [['visible','count']];&nbsp; &nbsp;&nbsp;while ($row_Recordset1 = mysqli_fetch_assoc($Recordset1) ){&nbsp; &nbsp; $data[] =[$row_Recordset1['visible'],(int) $row_Recordset1['count']];}&nbsp;在您的代码中,该行...$data[$i] = array((string) mysqli_fetch_array($Recordset1)['visible'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(int) mysqli_fetch_array($Recordset1)['count']);检索 2 行数据,一行用于零件visible,另一行用于零件count,这就是数据偏移的原因。
随时随地看视频慕课网APP
我要回答