简体   繁体   English

如何从mysql列中获取最大值并分配给php变量

[英]how to grab the max from mysql columns and assign to php variable

It is giving me errors when I run this code, I want to grab the max of my columns then eventually add 1 to it.当我运行此代码时,它给了我错误,我想获取列的最大值,然后最终向其中添加 1。 I want to be able to use new max after these operations are completed.这些操作完成后,我希望能够使用新的最大值。 I think my sytnax is wrong on the x = line我认为我的 sytnax 在 x = 行上是错误的

     $sql2 = "SELECT max(order_number) from t_item_list
     where template_item_id =  '$id'"

     $x = mysqli_fetch_array($sql2)
     $newmax = $x +1;

You are forgetting to execute the query.您忘记执行查询。 Here's your fixed code:这是您的固定代码:

$sql2 = "SELECT max(order_number) from t_item_list where template_item_id = '$id'";

// I assume here that you already have a database connection
$result = $connection->query($sql2); 

$x = mysqli_fetch_array($result);
$newmax = $x +1;

Also, mysqli_fetch_array($result) will probably return you an array.此外, mysqli_fetch_array($result)可能会返回一个数组。 You must retrieve the value according to it.您必须根据它检索值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM