DmitryB писал(а):Спасибо, срипты это следующий этап. Для меня сейчас стоит задача по работе с БД.
Дмитрий, с базами данных как раз и надо работать через скрипты. Это удобней и правильней.
/var/lib/asterisk/agi-bin/sql.php -
Код: Выделить всё
<?php
/* $Id: sql.php 11662 2011-03-04 00:28:02Z p_lindheimer $ */
// SQL Abstraction Layer for AGI Applications
// Original Release by Rob Thomas (xrobau@gmail.com)
// Copyright Rob Thomas (2009)
// Commands:
//
// $AGI = new AGI();
// $db = new AGIDB($AGI);
//
// $result = $db->escape($sql)
// Escapes any characters that could confuse the database and lead to SQL injection problems.
// You should use this on ANY browser-supplied or user-supplied input.
//
// $result = $db->sql($sql, $type)
// Returns the result of the SQL command $sql. This will die noisily if you
// try to do something that isn't portable between databases (eg ALTER TABLE
// or use CREATE with 'auto_increment') - use the alternate commands below,
// or design your database to be portable.
// $type specifies the return type -
// ASSOC for an Associative array
// NUM for a numeric array.
// BOTH for both in the same result
//
// Note this returns the ENTIRE result. So for example taken from routepermissions:
//
// $res = $db->sql("SELECT allowed,faildest FROM routepermissions WHERE exten='$cidnum' and routename='$routename'", "BOTH");
//
// if allowed and faildest return 'NO' and 'ext-vm,300', the result would look like this:
// $result = {
// [0] => {
// [0] = 'NO', // Only these with 'NUM' type
// [1] = 'ext-vm,300',
// 'allowed' => 'NO', // Only these with 'ASSOC' type
// 'faildest => 'ext-vm,300',
// }
// }
//
// if ($res[0]['allowed'] == 'NO') {
// $agi->goto($res[0]['faildest']);
// }
//
//
// $result = $db->rename_table($from, $to)
// Renames a table. Result is not null if an error occured, and the errorstr
// is in $result.
//
// $result = $db->add_col($tablename, $colname, $type)
// Add a column called $col_name of type $type to table $tablename. Result is not
// null if an error occured, and the errorstr is in $result.
//
// $result = $db->drop_col($tablename, $colname)
// Drops a column from table $tablename. Actually drops the column if using MySQL,
// recreates the table if using SQLite.
//
// $result = $db->alter_col($tablename, $colname, $type)
// Changes the type of a column. Changes it directly in MySQL, recreates the table
// if using SQLite
//
if (!class_exists('AGI')) {
}
class AGIDB {
// Database Variables from [globals]. Self explanatory.
private $dbhost;
private $dbuser;
private $dbpass;
private $dbfile;
private $dbname;
// sqlite3 needs some global variables to handle returns. They aren't needed
// to be defined here, but be aware that they are used by this module.
/* global $sql3holderAssoc; */
/* global $sql3holderNum; */
/* global $sql3holderRowNbr; */
private $agi; // A copy of the AGI class already running
private $db; // 'mysql', 'sqlite' or 'sqlite3'. Set in sql_database_connect, so we
// Public things that you might need to access
public $errstr; // Holder for error string
public $numrows; // Number of rows returned in the query
// Just in case someone REALLY wants to work around all the sanity checks here, these
// two variables are public, so you can use them if you REALLY must.
public $dbtype;
public $dbhandle;
function AGIDB($AGI=null) {
}
function sql_database_connect() {
// Determine DB type
// dl() is gone in php5, but since this will crash it anyhow, will just leave it as is
}
и так далее. Скрипт (выше) приведён не полностью.