0

i have an issue gettin' the value from a query:

public static function sa() {
 $resul = Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->execute();
 $got = mysql_query($result); 
 $res = $got['MAX(id)'] + 1;
 $rs='SA'.$res;

 return "{$rs}";
}

it always return SA1, but i want get the last id and after plus 1 so in this case i have the next autoincremental id from my id column.

For example: i am creating a new registry with the field SA0000005. This number is calculated getting the last autoincremental value plus 1.

thanks for your valuable help

1 Answer 1

1
$resul = Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->execute();
$got = mysql_query($result); // what are you even doing here

Apart from typos, that's not how you are supposed to use the query builder. Have you read the documentation on the query builder?

The probable reason why you always get SA1, is because the $got['MAX(id)'] expression is NULL. You add 1 to that. You want something like this.

// returns false on no-result, MAX('id') otherwise
Yii::app()->db->createCommand()->select('MAX(id)')->from('yii_availability')->queryScalar();
Sign up to request clarification or add additional context in comments.

1 Comment

thanks Elias, i was looking many solutions but no one with queryScalar, i have the correct result now. Thanks a lot again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.