Fetch DID from my MYSQL DB

I do something like this (If I’m understanding your request correctly)

When a call comes in I grab the DID and CallerId off of the incoming call and use them in a SQL query that give me back the information that I need to route the call.

If that’s what you are looking for, take a look at this:

[dynroute-5] ; DA

exten => s,1,NoOp(DYN-ROUTE-DA) ; Logging

exten => s,n,Set(ARRAY(dynrout,dynrou,dynroute)={MYSQL_LastConnect({CALLERID(num)})}); My mysql query returns three items

exten => s,n,GotoIf(["{dynroute}" = “”]?dynroute,3,1)

exten => s,n,Set(V=${dynroute}) ; must be set

exten => s,n,Goto(dynroute,1,1) ; code that actually handles the routing

[LastConnect]

prefix=MYSQL

dsn=asteriskcdrdb

readsql=SELECT max(c1.calldate) as calldate , c1.src, dst

readsql+= FROM cdr as c1

readsql+= where length(dst) <4 and length(dst) >2 and length(src) > 6 and

c1.src = ‘${ARG1}’ and (c1.dst < 900 and disposition = “ANSWERED”)

readsql+= group by c1.dst

readsql+= having date_add(calldate, interval 7 day) > now()

readsql+= order by calldate desc

readsql+= limit 5;