当我从数据库收到错误需要以适当的格式显示它

When i run multiple queries using foreach loop that time i get error from database. I need to get that error in to variable or array. When i get error that time the page is crash. But i want to show that error in proper format and also revert back other queries which are fired. Here i pass you code which i run.

$sql =  file_get_contents($path."abc.sql");
$sqls = explode(';', $sql);
array_pop($sqls);
foreach($sqls as $statement){
    $this->db->query($statement);
}

I have attached image of error message. enter image description here

Table is already created and i run below query.

CREATE TABLE IF NOT EXISTS `cli_group` (
  `id` int(11) NOT NULL,
  `name` char(20) NOT NULL DEFAULT '0',
  `description` varchar(100) NOT NULL,
  `reseller_id` int(11) DEFAULT '0' COMMENT 'Accoun',
  `assignment_method` tinyint(1) NOT NULL DEFAULT '0',
  `status` tinyint(1) NOT NULL DEFAULT '1',
  `creation_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `last_access_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `cli_group`
  ADD PRIMARY KEY (`id`),
  ADD KEY `reseller` (`reseller_id`);

ALTER TABLE `cli_group`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

When you run that script, if the cli_group table already exists, it does not get created again because of the EXISTS clause in the CREATE TABLE statement.

However, this of course means that its primary key also already exists and you cannot create a new one via the ALTER TABLE statement - the error message is telling you that you cannot add another primary key definition to a table which already has one. If you wanted to do that you'd have to remove the old key first.

However, I don't know your exact business requirements here but I think it would almost certainly make more sense to simply declare the primary key within the CREATE TABLE statement rather than using a separate ALTER TABLE statement. Then if it already exists, nothing gets created, including the primary key. That way you can avoid this problem of uncertainty about the status of the table.