在使用准备好的语句进行查询的过程中,如何使用PHP填充数组

k4kuz0

我将在此处发布整个php,但以下是我遇到的麻烦并着重关注的部分,即数组的实际填充量。

$mysqli = new mysqli('localhost', 'root', '', 'dota_site_test');
if(mysqli_connect_errno()) {
    echo "Connection Failed: " . mysqli_connect_errno();
    exit();
}
$player_id = 123;//change later

if($stmt = $mysqli -> prepare("SELECT Match_ID,Hero,Result,GameMode,MMR FROM matches WHERE Player_ID = ?")) {

    $stmt -> bind_param("i", $player_id);

    $stmt -> execute();

    $stmt -> bind_result($match_id, $hero, $result, $gamemode, $mmr);
    //adds the table setup only when data is fetched ok (above)
    echo "<table id='matches_table'>";
    echo "<thead>
                <tr>
                    <th>Hero</th>
                    <th>Result</th>
                    <th>Game Mode</th>
                    <th>MMR</th>
                    <th>Diff</th>
                </tr>
            </thead>";
    $i = 1;
    $mmr2 = 0;
    $grapharray = array();
    while($stmt -> fetch()){
    //While there is still data being fetched, make the table rows below
        $mmr1 = $mmr;       //first mmr calc value = mmr from fetch
        $diff = 0;          //difference defined as 0
        if ($mmr2 != 0){     //if the MMR2 has a value (from 2nd loop)
            $diff = $mmr1-$mmr2;   
        }
        $mmr2 = $mmr1;      //so that both values can be present in the loop

        echo "<tr><td>$hero</td>
                <td>$result</td>
                <td>$gamemode</td>
                <td data-id='$i'>$mmr</td>
                <td>$diff</td></tr>";   
        $i++;
        $grapharray[$stmt['$hero']] = $stmt['$mmr'];

    }

    echo "</table>";

    $stmt -> close();

}
$mysqli->close();

?>

这是我关注的部分,我试图使用while循环,该循环使用数据填充HTML表,还使用该循环将数据单元格(MMR和Hero名称)放入数组中,以供以后创建图形时使用。

这是更集中的部分:

while($stmt -> fetch()){
    //While there is still data being fetched, make the table rows below
        $mmr1 = $mmr;       //first mmr calc value = mmr from fetch
        $diff = 0;          //difference defined as 0
        if ($mmr2 != 0){     //if the MMR2 has a value (from 2nd loop)
            $diff = $mmr1-$mmr2;   
        }
        $mmr2 = $mmr1;      //so that both values can be present in the loop

        echo "<tr><td>$hero</td>
                <td>$result</td>
                <td>$gamemode</td>
                <td data-id='$i'>$mmr</td>
                <td>$diff</td></tr>";   
        $i++;
        $grapharray[$stmt['$hero']] = $stmt['$mmr'];

    }

我不确定如何用此循环制作一个数组,因为出现错误:

Fatal error: Cannot use object of type mysqli_stmt as array in C:\xampp\htdocs\Dota2HomePage\d2index.php on line 79

我想我做错了什么!

巴特·弗里德里希斯

您应该使用绑定变量:

while($stmt -> fetch()){
    ...
    $grapharray[$hero] = $mmr;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我如何使用implode()在PHP中使用准备好的语句将多个整数插入IN语句中?

Sqlx使用准备好的语句获取

使用准备好的语句从SQL表中进行SELECT *

如何使用准备好的语句更新阵列的单个元素?

如何使用准备好的语句动态绑定参数?

存储过程中的准备好的语句不起作用

在PHP和MySQLi中使用使用准备好的语句和参数化查询

使用准备好的语句对插入内容进行消毒

在Postgresql存储过程中使用准备好的语句或动态命令?

如何在Sqflite中为Flutter使用准备好的语句

如何使用准备好的语句选择IP地址

如何在单个php准备好的语句上同时使用SELECT和INSERT查询?

如何在PHP中的PostgreSQL数据库上使用多个参数编写准备好的语句

使用准备好的语句在MySql中插入(DATE(now))

在magento中,如何使用core_resource编写准备好的语句?

如何在Android sqlite中使用准备好的语句?

PHP函数使用准备好的SQL语句-使用爆破输入以逗号分隔的数组值作为函数的参数

EXTJS 5和PHP使用准备好的语句创建

我如何使用准备好的语句更新mysql数据库中的值

如何使用准备好的语句在MySQL中插入多行而不插入空白行

使用准备好的语句写入 mysql

使用准备好的语句进行 SQL 注入

如何使用准备好的语句从 mysql 中获取多行

无法在 MySQLi 中使用准备好的语句运行查询

使用准备好的语句 mysqli 获取查询结果

使用准备好的语句构建 PDO 动态查询

使用 PHP 和 MSSQL 准备好的语句

使用 php 和准备好的语句写入 mysql

是否可以(通过 PHP)使用先前在 mysql CLI 中声明的准备好的语句?