has how to execute a query within a user-defined function in php?

0

I have code like this:

function apagar(){

    $conexao = mysqli_connect('localhost','usuario','senha');
    $db = mysqli_select_db("banco");    

    $delete = mysqli_query("TRUNCATE table sap_reportes.zempe019");
    return $delete;

    //return 'teste';
}

//script do relatório.

apagar($delete);

//fim do relatório.
The problem is that Truncate does not work, I am running this script in cmd, this script server to generate reports so alright he is generating the normal report and this is emailing the problem is that at the end of the report I would like to delete the table already tried everything else I do not know why the truncate does not work after running the whole script at the beginning of the script it even works it would need more that this truncate executed at the end of the report so I did this function in php and I called her at the end of the report.

    
asked by anonymous 11.04.2016 / 14:11

1 answer

-1

To make a tunnel, I prefer to use PDO:

 class Database {

        private static $servidor = 'localhost'; // Servidor, no caso poderia ser também localhost
        private static $usuario = 'root'; // usuário do banco de dados
        private static $senha = 'senha'; // senha do banco de dados
        private static $banco = 'banco'; // nome do banco de dados
        private static $instance = null;

        public static function getConnection() {
            if (!self::$instance instanceof PDO) {
                try {
                    self::$instance = new PDO('mysql:host=' .
                    self::$servidor . ';dbname=' . 
                    self::$banco, 
                    self::$usuario, 
                    self::$senha);
                } catch (PDOException $exc) {
                    echo "Erro ao conectar :: {$exc->getMessage()}";
                }
            }
            return self::$instance;
        }

        public function fetchAll($query) {
            $con = self::getConnection();

            $stmt = $con->prepare($query);

            $this->execute($stmt);

            if ($stmt->rowCount()) {
                return $stmt->fetchAll(PDO::FETCH_OBJ);
            } else {
                return false;
            }
        }

        public function execute(PDOStatement $stmt, array $data = null) {
            try {
                if (isset($data)) {
                    $stmt->execute($data);
                } else {
                    $stmt->execute();
                }
            } catch (PDOException $exc) {
                echo $exc->getTraceAsString();
            }
        }

        public function action($sql, array $data) {
            $con = self::getConnection();
            $stmt = $con->prepare($sql);
            $this->execute($stmt, $data);
            if ($stmt->rowCount()) {
                return true;
            } else {
                return false;
            }
        }
    }

$conexao = new Database();

//listando...
$collection = $conexao->fetchAll("SELECT * FROM tabela");

foreach ($collection as $data) {
      echo $data['campo'] . '<br>';
}

//deletando...
 $deletou = $conexao->action('DELETE FROM tabela WHERE id=:id',array('id' => (int) $id));
 if ($deletou) {
   echo "apagou!";
 }

//inserindo
$insert = $conexao->action("INSERT INTO tabela (campo1, campo2) values (:campo1,:campo2)",array('campo1'=>'valor1','campo2'=>'valor2'));
 if ($insert) {
   echo "gravou!";
 }
//atualizando
$update = $conexao->action("UPDATE tabela SET campo1=:campo1, campo2=:campo2 where id=:id",array('campo1'=>'valor1','campo2'=>'valor2','id' => (int) $id));
 if ($update) {
   echo "atualizou!";
 }
    
11.04.2016 / 15:45