duanba8173
2019-05-16 22:26
浏览 71
已采纳

从数据库中选择列数据(无法转换为数字?)

I need to be able to compare values from a column, not the number of times a value appears in the columns. I have multiples tables that all have the same 20 column 'title' entries, but another column 'position_order' are different values of numbers. I have a base table with the 'correct' values, and I want to go through each name in the column 'title' and calculate the difference between their 'position_order' and my base case table's 'position_order'

I believe I have it all working but when I query the database to find what value it has stored for the column 'position_order' for some reason it won't return as a number that I can do calculations on.

<?php
    echo "
        <h3 class='text-center'>Table Name</h3>
        <table class='table table-bordered'>
            <tr>
                <th>#</th>
                <th>Title</th>
                <th>Score</th>
            </tr>
            <tbody class='row_position'>"?>
            <?php

            require('db_config.php');
            $tablename = $_SESSION['username'] . "_laliga";
            $_SESSION['tablename'] = $tablename;
            $sql = "SELECT * FROM $tablename ORDER BY position_order";
            $users = $mysqli->query($sql);
            while($user = $users->fetch_assoc()){
            $sql1 = "SELECT `position_order` FROM '".$tablename."' WHERE `title` LIKE '".$user['title']."' ";
            $sql2 = "SELECT `position_order` FROM `laliga` WHERE `title` LIKE '".$user['title']."' ";
            $position1 = $mysqli->query($sql1);

            $position2 = $mysqli->query($sql2);

            ?>


<tr  id="<?php echo $user['id'] ?>">
                <td><?php echo $user['position_order'] ?></td>
                <td><?php echo $user['title'] ?></td>
                <td><?php echo abs($position1-$position2); ?></td>
            </tr>
        <?php } ?>
        </tbody>
    </table>

Here is the error log

[16-May-2019 22:19:48 UTC] PHP Notice: Object of class mysqli_result could not be converted to number in /Applications/MAMP/htdocs/user.php on line 75

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duankeng2026 2019-05-16 22:38
    已采纳

    This:

    $result = $mysqli->query($sql1);
    if ( ! $result ) {
       throw new Exception('Query 1 failed');
    }
    

    Returns a mysqli_result object, not a number. You must extract the actual results from it.

    if ( $result->num_rows ) {
        $row = $result->fetch_assoc();
        $position1 = $row['position_order'];
    }
    $result->close();
    
    $result = $mysqli->query($sql2);
    if ( ! $result ) {
       throw new Exception('Query 2 failed');
    }
    
    if ( $result->num_rows ) {
        $row = $result->fetch_assoc();
        $position2 = $row['position_order'];
    }
    $result->close();
    if ( isset($position1) && isset($position2) ) {
       $abs = abs($position1-$position2);
    } else {
       $abs = null;
    }
    
    // output here
    
    点赞 打赏 评论
  • duandazhen7306 2019-05-16 22:33

    See the documentation for mysqli_query: https://www.php.net/manual/en/mysqli.query.php

    Return Values

    For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

    $position1 and $position2 are object instances, not fields from the database. Haven't used mysqli in forever but you probably want fetch_all instead?

    点赞 打赏 评论

相关推荐 更多相似问题