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

时间:2014-07-03 07:25:50

标签: 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

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

在名称周围加上双引号使其成为文字字符串,而不是列名。摆脱报价。此外,没有理由做3个不同的查询来获取同一行的字段。而且您忘记调用mysql_fetch_assoc()来实际获取结果中的行。

$result = mysql_query("SELECT total_flag1, total_flag2, total_draw
                       FROM balance 
                       ORDER BY my_date DESC
                       LIMIT 1") or die(mysql_error());
$row = mysql_fetch_assoc($result);
$sub_total_1 = $row['total_flag1'];
$sub_total_2 = $row['total_draw'];
$sub_total_3 = $row['total_flag2'];