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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?