我正在从代码中增加一个ID。当我从数据库中检索最大值时,它没有分配给变量。
我从数据库SQL测试查询,然后显示最大值。但是当我回声时它不会显示。
这是我的代码,
<html>
<body>
<form enctype="multipart/form-data" method="post" action="test_issue.php">
<input name="submit" type="submit" id="submit" value="submit" />
</form>
</body>
</html>
<?php
include ("connection.php");
if(isset($_POST['submit']))
{
// $queryselect = "SELECT issued_id FROM pass WHERE pass_id=8";
$queryselect= "SELECT MAX(issued_id) FROM pass WHERE status='Issued'";
$last_sec_result = mysqli_query($connection,$queryselect );
if($last_sec_result!=0)
{
$last_sec_value = mysqli_fetch_array($last_sec_result);
$sec_id_no = $last_sec_value['issued_id'];
echo $sec_id_no;
}
else
{
echo $last_sec_result ;
echo "database query failed please check data you enter.";
}
// echo $sec_id_no;
}
?>
当我使用简单的选择查询(SELECT issued_id FROM pass WHERE pass_id=8
)检索值时,它会显示该值。我坚持这一点。谁能帮我。
答案 0 :(得分:1)
您的查询定义了列MAX(issued_id)
,但没有别名,因此,在获取的记录中,它具有一些奇特的名称,例如... MAX(issued_id)
。
尝试像这样更改您的查询(注意AS issued_id
部分):
SELECT MAX(issued_id) AS issued_id FROM pass WHERE status='Issued'
这将确保获取的记录具有issued_id
字段,并且您的以下代码将能够提取该记录。
答案 1 :(得分:0)
1- mysql_query()不返回整数,因为您的条件正在等待 2-使用别名来确定返回结果集中的索引
include ("connection.php");
if(isset($_POST['submit']))
{
// $queryselect = "SELECT issued_id as issued_id FROM pass WHERE pass_id=8";
$queryselect= "SELECT MAX(issued_id) as issued_id FROM pass WHERE status='Issued'";
$last_sec_result = mysqli_query($connection,$queryselect );
if($last_sec_result !== null)
{
$last_sec_value = mysqli_fetch_array($last_sec_result);
$sec_id_no = $last_sec_value['issued_id'];
echo $sec_id_no;
}
else
{
echo $last_sec_result ;
echo "database query failed please check data you enter.";
}
// echo $sec_id_no;
}
答案 2 :(得分:0)
因此,如果以前的答案没有为您提供预期的解决方案,那么
没有状态设置为完全相同的内容'Issued'
的行,
这就是查询返回空结果数组的原因。
SELECT issued_id, status FROM pass;
# must return some rows
SELECT MAX(issued_id) AS issued_id FROM pass;
# must return some numbers
SELECT MAX(issued_id) AS issued_id FROM pass WHERE status='Issued';
# should return expected value