<p>How do I get Asterisk to forward incoming calls based on matching the incoming call number with a number to forward to? Both numbers are stored in a MySQL database.</p>

[{'answer_id': 69689, 'author': 'willurd', 'author_id': 1943957, 'author_profile': 'https://Stackoverflow.com/users/1943957', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://scottstuff.net/blog/articles/2004/08/09/database-driven-call-forwarding-with-asterisk" rel="nofollow noreferrer">This article</a> should do the trick. It\'s about 3 lines of code and some simple queries to add and remove forwarding rules.</p>\n'}, {'answer_id': 75492, 'author': 'Jeremy Wadhams', 'author_id': 8995, 'author_profile': 'https://Stackoverflow.com/users/8995', 'pm_score': 2, 'selected': False, 'text': '<p>Sorry for the long code sample, but more than half of it is debugging code to help you get it set up.</p>\n\n<p>I\'m assuming your server already has a modern version of PHP (at <code>/usr/bin/php</code>) with the PDO library, and that you have a database table named <code>fwd_table</code> with columns <code>caller_id</code> and <code>destination</code>.</p>\n\n<p>In /var/lib/asterisk/agi-bin get a copy of the <a href="http://sourceforge.net/projects/phpagi/" rel="nofollow noreferrer">PHP AGI</a> library. Then create a file named something like <code>forward_by_callerid.agi</code> that contains:</p>\n\n<pre><code>#!/usr/bin/php\n&lt;?php\nini_set(\'display_errors\',\'false\'); //Supress errors getting sent to the Asterisk process\n\nrequire(\'phpagi.php\');\n$agi = new AGI();\n\ntry {\n    $pdo = new PDO(\'mysql:host=\'.$db_hostname.\';dbname=\'.$db_database.\';charset=UTF-8\', $db_user, $db_pass);\n} catch (PDOException $e) {\n    $agi-&gt;conlog("FAIL: Error connecting to the database! " . $e-&gt;getMessage());\n    die();\n}\n\n$find_fwd_by_callerid = $pdo-&gt;prepare(\'SELECT destination FROM fwd_table WHERE caller_id=? \');\n\n$caller_id = $agi-&gt;request[\'agi_callerid\'];\n\nif($callerid=="unknown" or $callerid=="private" or $callerid==""){\n    $agi-&gt;conlog("Call came in without caller id, I give up");\n    exit;\n}else{\n    $agi-&gt;conlog("Call came in with caller id number $caller_id.");\n}\n\nif($find_fwd_by_callerid-&gt;execute(array($caller_id)) === false){\n    $agi-&gt;conlog("Database problem searching for forward destination (find_fwd_by_callerid), croaking");\n    exit;\n} \n$found_fwds = $find_fwd_by_callerid-&gt;fetchAll();\nif(count($found_fwds) &gt; 0){\n    $destination = $found_contacts[0][\'destination\'];\n    $agi-&gt;set_variable(\'FWD_TO\', $destination);\n\n    $agi-&gt;conlog("Caller ID matched, setting FWD_TO variable to \'\'");\n}\n\n?&gt;\n</code></pre>\n\n<p>Then from the dial plan you can call it like this:</p>\n\n<pre><code>AGI(forward_by_callerid.agi)\n</code></pre>\n\n<p>And if your database has a match, it will set the variable <code>FWD_TO</code> with goodness.  Please edit your question if you need more help getting this integrated into your dial plan.</p>\n'}, {'answer_id': 1869230, 'author': 'Aaron', 'author_id': 11176, 'author_profile': 'https://Stackoverflow.com/users/11176', 'pm_score': 2, 'selected': True, 'text': '<p>The solution I was looking for ended up looking like this:</p>\n\n<pre><code>[default]\nexten =&gt; _X.,1,Set(ARRAY(${EXTEN}_phone)=${DTC_ICF(phone_number,${EXTEN})})\nexten =&gt; _X.,n(callphone),Dial(SIP/metaswitch/${${EXTEN}_phone},26)\nexten =&gt; _X.,n(end),Hangup()\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/69676', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11176/']