mysqli_assoc显示结果,但aslo参数字符串错误

时间:2019-03-15 05:10:20

标签: php mysqli

我已将'$ id'更改为“'。$ id。'”,并且仍然存在相同的错误..结果正确显示,其中ID =数据库表中的ID,但同时也显示错误

  

警告:mysqli_fetch_assoc()期望参数1为mysqli_result,   第85行给出的字符串

第85行

 while($row=mysqli_fetch_assoc($result)) {

完整代码

$id = $_GET['GetID'];

$connection = mysqli_connect("*****", "", "*****", "*****");
    if (!$connection) {
        die("Database connection failed: " . mysqli_connect_error());
    }

        $query = "SELECT * FROM bets WHERE id='$id'";
        $result = mysqli_query($connection, $query);

        while($row=mysqli_fetch_assoc($result)) {

            $betDate = $row['betDate'];
            $tipster = $row['tipster'];
            $sport = $row['sport'];
            $meeting = $row['meeting'];
            $time = $row['time'];
            $stakeName = $row['stakeName'];
            $odds = $row['odds'];
            $stakeType = $row['stakeType'];
            $stakePlaced = $row['stakePlaced'];
            $result = $row['result'];
            $return = $row['return'];
            $profit = $row['profit'];



        }
        mysqli_close($connection);

        ?>

HTML

<div class="form-group">
                                <label for="id">Bet ID</label>
                                <input type="text" class="form-control mb-2" placeholder="id" name="id" readonly value="<?php echo $id ?> ">
                            </div>

                            <div class="form-group">
                                <label for="betDate">Date (YYYY/MM/DD)</label>
                            <input type="text" class="form-control mb-2" placeholder="betDate" id="betDate" name="betDate" pattern="(?:20|20)[0-9]{2}[- /](?:(?:0[1-9]|1[0-2])[- /](?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])[- /](?:30))|(?:(?:0[13578]|1[02])-31))" value="<?php echo $betDate ?>" readonly>
                            </div>

                            <div class="form-group">
                                <label for="tipster">Tipster</label>
                            <input type="text" class="form-control mb-2" placeholder="tipster" id="tipster" name="tipster" value="<?php echo $tipster ?>">
                            </div>

                            <div class="form-group">
                                <label for="sport">Sport</label>
                            <input type="text" class="form-control mb-2" placeholder="sport" id="sport" name="sport" value="<?php echo $sport?>">
                            </div>

                            <div class="form-group">
                                <label for="meeting">Meeting</label>
                            <input type="text" class="form-control mb-2" placeholder="meeting" id="meeting" name="meeting"  value="<?php echo $meeting ?>">
                            </div>

                            <div class="form-group">
                                <label for="time">Time</label>
                            <input type="text" class="form-control mb-2" placeholder="time" id="time" name="time" value="<?php echo $time ?>">
                            </div>

                            <div class="form-group">
                                <label for="stakeName">Stake Name</label>
                            <input type="text" class="form-control mb-2" placeholder="stakeName" id="stakeName" name="stakeName" value="<?php echo $stakeName ?>">
                            </div>

                            <div class="form-group">
                                <label for="odds">Odds</label>
                            <input type="text" class="form-control mb-2" placeholder="odds" id="odds" name="odds" value="<?php echo $odds ?>" readonly>
                            </div>

                            <div class="form-group">
                                <label for="stakeType">Stake Type</label>
                            <input type="text" class="form-control mb-2" placeholder="stakeType" id="stakeType" name="stakeType" value="<?php echo $stakeType ?>">
                            </div>

                            <div class="form-group">
                                <label for="stakePlaced">Stake Placed</label>
                            <input type="text" class="form-control mb-2" placeholder="stakePlaced" name="stakePlaced" id="stakePlaced" value="<?php echo $stakePlaced ?>" readonly>
                            </div>

                            <div class="form-group">
                                <label for="result">Result</label>
                            <input type="text" class="form-control mb-2" placeholder="result" name="result" id="result" pattern="Win|Loss" value="<?php echo $result ?>">
                            </div>

                            <div class="form-group">
                                <label for="return">Return</label>
                            <input type="text" class="form-control mb-2" placeholder="return" id="return" name="return" value="<?php echo $return ?>" readonly>
                            </div>

                            <div class="form-group">
                                <label for="return">Profit</label>
                            <input type="text" class="form-control mb-2" placeholder="profit" name="profit" id="profit" value="<?php echo $profit ?>" readonly>
                            </div>

1 个答案:

答案 0 :(得分:0)

$resultfalse。这意味着查询无法从表中获取给定ID的任何内容。

最好将while块放在if($result)条件内。

$result = mysqli_query($connection, $query);
if($result) {
    while($row=mysqli_fetch_assoc($result)) {
        $betDate = $row['betDate'];
        $tipster = $row['tipster'];
        $sport = $row['sport'];
        $meeting = $row['meeting'];
        $time = $row['time'];
        $stakeName = $row['stakeName'];
        $odds = $row['odds'];
        $stakeType = $row['stakeType'];
        $stakePlaced = $row['stakePlaced'];
        $result = $row['result'];
        $return = $row['return'];
        $profit = $row['profit'];
    }
}