我试图获得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);
?>


发布于 2014-07-03 07:31:48
在名称周围加上双引号使其成为文字字符串,而不是列名。去掉引号。此外,没有理由执行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'];发布于 2014-07-03 07:35:34
要获取正在使用的«sub_total_N»值,此函数将返回“用于SELECT、SHOW、DESCRIBE、EXPLAIN和其他返回结果集的语句,mysql_query()在成功时返回资源,错误时返回FALSE”。
您必须获取资源才能获得值。
注意:使用查询而不是mysql_query,这是不推荐的。
https://stackoverflow.com/questions/24547513
复制相似问题