php - SELECT 列中的最后一个值始终为 null 或 false SQL

标签 php mysql sql

我试图获取 SQL 中 3 列的最后一个值,我尝试了许多查询,但都返回 null 或 false,

数据库可以进行更新和插入,但我无法获取这三个值并与 php 变量匹配

    $sub_total_1 = mysql_query('SELECT "total_flag1" FROM balance ORDER BY my_date DESC LIMIT 1');
    $total_1 = $val_1 + $sub_total_1;

    $sub_total_2 = mysql_query('SELECT "total_draw" FROM balance ORDER BY my_date DESC LIMIT 1');
    $total_2 = $val_2 + $sub_total_2;

    $sub_total_3 = mysql_query('SELECT "total_flag2" FROM balance ORDER BY my_date DESC LIMIT 1');
    $total_3 = $val_3 + $sub_total_3;

这是完整的 PHP 代码和我如何制作这个数据库的屏幕,谢谢。

<?php

$porcentaje_ofrecido = 1.7;

$val_1 = floatval(addslashes($_POST['first']));
$val_2 = floatval(addslashes($_POST['second']));
$val_3 = floatval(addslashes($_POST['third']));

include_once('config.php');

$mysqlConnection = mysql_connect($server, $username, $password);
if (!$mysqlConnection) {
    $response = array(
        "codigo" => "nok",
        "mensaje" => "<h3>Hubo un error en la base de datos</h3>
                    <p>Intenta más tarde</p>"
    );
} else {
    mysql_select_db($database, $mysqlConnection);
    $date = date_create();
    $date = date_format($date, 'Y-m-d H:i:s');

    if (is_numeric($val_1) && is_numeric($val_2) && is_numeric($val_3)) {


        $sub_total_1 = mysql_query('SELECT "total_flag1" FROM balance ORDER BY my_date DESC LIMIT 1');
        $total_1 = $val_1 + $sub_total_1;

        $sub_total_2 = mysql_query('SELECT "total_draw" FROM balance ORDER BY my_date DESC LIMIT 1');
        $total_2 = $val_2 + $sub_total_2;

        $sub_total_3 = mysql_query('SELECT "total_flag2" FROM balance ORDER BY my_date DESC LIMIT 1');
        $total_3 = $val_3 + $sub_total_3;

        $sub_total = $sub_total_1 + $sub_total_2 + $sub_total_3;
        $total = $total_1 + $total_2 + $total_3;

        $case1 = ($total_1 * $porcentaje_ofrecido * (-1)) + $total_2 + $total_3;
        $case2 = ($total_2 * $porcentaje_ofrecido * (-1)) + $total_1 + $total_3;
        $case3 = ($total_3 * $porcentaje_ofrecido * (-1)) + $total_1 + $total_2;

        if ($case1 > 0 && $case2 > 0 && $case3 > 0) {
            $sql = "INSERT into balance (id,flag1,draw,flag2,total_flag1,total_draw,total_flag2,total,succes,date) VALUES ('','$val_1','$val_2','$val_3','$total_1','$total_2','$total_3','$total',1,'$date')";
            $res = mysql_query($sql);
            if ($res) {
                $sql2 = "UPDATE totales SET sub_total1= $total_1 ,sub_total2= $total_2 ,sub_total3= $total_3 ,total=$total";
                $res2 = mysql_query($sql2);
                if ($res2) {
                    $response = array(
                        "codigo" => "ok",
                        "mensaje" => "<h2>Hemos guardado tu registro</h2>
                          <p>Muchas gracias.</p>"
                    );
                } else {
                    $response = array(
                        "codigo" => "nok",
                        "mensaje" => "<h2>Hubo un error con el registro</h2>
                <p>Lamentablemente hemos tenido problemas al registrar tus datos</p>
                >"
                    );
                }
            } else {
                $response = array(
                    "codigo" => "nok",
                    "mensaje" => "<h2>Hubo un error con el registro</h2>"
                );
            }
        } else {
            $response = array(
                "codigo" => "nok",
                "mensaje" => array('14', $sub_total_1, $val_1, $total_1, $porcentaje_ofrecido, $total_2, $total_3)
            );
        }
    } else {
        $response = array(
            "codigo" => "nok",
            "mensaje" => "<h2>Hubo un error x.x</h2>"
        );
    }
}
echo json_encode($response);
?>

http://i.stack.imgur.com/ytFcf.png http://i.stack.imgur.com/nvvW6.png

最佳答案

获取“sub_total_N”值您正在使用mysql_query,并且此函数返回“对于返回结果集的SELECT、SHOW、DESCRIBE、EXPLAIN 和其他语句,mysql_query() 在成功时返回资源,在错误时返回FALSE。”

您必须获取资源才能获取值。

注意:使用 mysqli_query它已被弃用,而不是 mysql_query。

关于php - SELECT 列中的最后一个值始终为 null 或 false SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24547513/

相关文章:

python - 使用 python psychopg2 获取数据库中包含特定字符串的所有变量的计数

c# - IQueryable with Foreach 结果

php - 试图弄清楚 MySQL 查询的作用(不是我的)

javascript - 如何将 javascript 变量设置为静态 json 数据?

php - 在mysql存储过程中使用fetch table获取表信息

php - 需要连接不同列上的 3 个表

MYSQL计数查询错误

sql - 如何从 SQL Server 中的表中获取行索引?

java - 通过POST发送参数到php文件并读取php文件的echo

php - 代理后面的 curl 发布文件返回错误