≡ Fetching Multiple Rows
I’ll admit, my experience with MySQL through Asterisk is somewhat limited. It might seem obvious for some, but I struggled trying to fetch multiple rows from a record set. Once I stumbled upon the answer I was shocked with the simplicity of it.
[mysql]
exten => _1NXXNXXXXXX,1,MYSQL(Connect conn localhost dbuser dbpass dbname)
exten => _1NXXNXXXXXX,2,MYSQL(Query resultid ${conn} SELECT\ value\,\ type\ FROM\ table\ WHERE\ extension=\'${EXTEN}\')
exten => _1NXXNXXXXXX,3,MYSQL(Fetch fetchid ${resultid} val type)
exten => _1NXXNXXXXXX,4,GotoIf($["${fetchid}" = "1"]?5:7)
exten => _1NXXNXXXXXX,5,Verbose(1| value: ${val} | type: ${type})
exten => _1NXXNXXXXXX,6,Goto(3)
exten => _1NXXNXXXXXX,7,MYSQL(Clear ${resultid})
exten => _1NXXNXXXXXX,8,MYSQL(Disconnect ${conn})
exten => _1NXXNXXXXXX,9,Return
The key is line 4 and 6. If there is a result found, we go to the verbose call and print the stuff grabbed from MySQL. Then line 6 sends you back to grab another row - Asterisk automatically picks up at the next row so you don’t need to increment the loop manually.
If there are no records - or we’ve reached the end of the results, we disconnect from MySQL and stop the loop.
Hopefully this will save someone some time…
- the evil genius


