我正在为Asterisk CDR表创建PHP接口,但是我在PHP中创建的表没有正确填充。
我能够成功地将 CDR 保存到由 XAMPP 运行的远程数据库中。
下面我附上SQL代码 - 由Asterisk的MariaDB自动创建
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */;
--
-- 数据库:
cdr_ex
--
-- 表
cdr
的表结构CREATE TABLE `cdr` ( `calldate` datetime NOT NULL, `clid` varchar(80) NOT NULL, `src` varchar(80) NOT NULL, `dst` varchar(80) NOT NULL, `dcontext` varchar(80) NOT NULL, `channel` varchar(80) NOT NULL, `dstchannel` varchar(80) NOT NULL, `lastapp` varchar(80) NOT NULL, `lastdata` varchar(80) NOT NULL, `duration` int(11) NOT NULL, `billsec` int(11) NOT NULL, `disposition` varchar(80) NOT NULL, `amaflags` int(11) NOT NULL, `accountcode` varchar(80) NOT NULL, `userfield` varchar(80) NOT NULL, `uniqueid` varchar(32) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- 转储表
cdr
的数据INSERT INTO `cdr` (`calldate`, `clid`, `src`, `dst`, `dcontext`, `channel`, `dstchannel`, `lastapp`, `lastdata`, `duration`, `billsec`, `disposition`, `amaflags`, `accountcode`, `userfield`, `uniqueid`) VALUES ('2019-04-16 09:43:29', '"t1" <110>', '110', 's', 'from-sip-external', 'SIP/XXX.XXX.X.XXX-00000000', '', 'Playback', 'ss-noservice', 7, 6, 'ANSWERED', 3, '', '', 'XXXXXXXXXX.0');
--
-- 转储表的索引
-- -- 表
cdr
的索引-- ALTER TABLE `cdr` ADD PRIMARY KEY (`uniqueid`); COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
这是我的PHP代码:
<body>
<h1>CDR</h1> <!-- heading -->
<div class="mainBody">
<?php
$query="SELECT 'calldate','dst','lastapp', 'duration', 'billsec', 'disposition' FROM cdr";
$cdr = mysqli_query($db,$query);
if(mysqli_num_rows($cdr)>0) //if exist print the table consisting of the all the fields from cdr table
{
echo "<table border='1'>
<tr>
<th>Call Date</th>
<th>Destination</th>
<th>Connection type</th>
<th>Duration</th>
<th>Billed time</th>
<th>Status</th>
</tr>";
while($row=mysqli_fetch_array($cdr))
{
echo "<tr>";
echo "<td><div name='cdate'>" . $row['calldate'] . "</div></td>";
echo "<td><div name='destination'>" . $row['dst'] . "</div></td>";
echo "<td><div name='ctype'>" . $row['lastapp'] . "</div></td>";
echo "<td><div name='cduration'>" . $row['duration'] . "</div></td>";
echo "<td><div name='btime'>" . $row['billsec'] . "</div></td>";
echo "<td><div name='cstatus'>" . $row['disposition'] . "</div></td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo '<p style="color:red">Nothing to display</p>';
}
$db->close(); //close the db
?>
</body>
输出。不是用实际内容填充表,而是在所有字段中获取表属性名称。(我不能包括图片)。
有人知道我做错了什么吗?其他表(用户创建)正在正确填充,所以我猜Asterisk表可能受到保护。
我不知道你想得到什么,但要 doc mysqli_fetch_array返回带有数字索引的数组,而不是哈希。
https://www.w3schools.com/php/func_mysqli_fetch_array.asp