2015-06-23 29 views
-2

当我运行这段代码时,它给了我错误,我想抓住我的列的最大值,然后最后给它加1。我希望在这些操作完成后能够使用新的最大值。我认为我的sytnax在x =行上是错误的如何从mysql列中获取最大值并分配给php变量

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

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

'$ x'是一个数组。你不能用这样的数组做数学运算,而且你也不能从查询字符串中获取数据。这只是一些SQL。直到您在数据库中执行它才是无用的。 –

+0

[并且此时出现...?](http://php.net/manual/en/mysqli.query.php) –

+0

[并且您正在使用哪个...?](https:// php .net/mysqlinfo.api.choosing) –

回答

2

您忘记执行查询了。这是你的固定代码:

$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; 

此外,mysqli_fetch_array($result)可能会回到你的数组。您必须根据它检索值。

+0

此答案旁边没有绿色勾号? –

相关问题