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

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

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Matlab问题解答有两个问题
  • ¥50 Oracle Kubernetes服务器集群主节点无法访问,工作节点可以访问
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架
  • ¥15 有关sql server business intellige安装,包括SSDT、SSMS。
  • ¥15 stm32的can接口不能收发数据
  • ¥15 目标检测算法移植到arm开发板
  • ¥15 利用JD51设计温度报警系统
  • ¥15 快手联盟怎么快速的跑出建立模型