0

I've been trying to integrate a procedure together with PHP for a while, but when calling it, PHP still has errors:


Overview:

$database = null;

 function Conecta($server, $user, $port, $bd){
  $GLOBALS['database'] = mysql_connect($server, $user, $port);
  mysql_select_db($bd);
  if ($GLOBALS['database']) {
   return $GLOBALS['database'];
  } else {
      aviso("ERRO!");
  }
 }

 function aviso($texto){
  ?>
   <script>
    alert("<?php echo $texto ?>");
   </script>
  <?php
 }

 function CadastrarFuncionario($nome_fun, $cargo_fun, $nascimento_fun, $cpf_fun, $rg_fun, $estado_fun, $salario_fun){
  $res = $GLOBALS['database']->query("SET @nomefun = '".$nome_fun."'");
  if ($res0) {
   aviso("foi");
  }
  $res .= $GLOBALS['database']->query("SET @cargofun = '".$cargo_fun."'");
  $res2 = $GLOBALS['database']->query("SET @nascimentofun = '".$nascimento_fun."'");
  $res3 = $GLOBALS['database']->query("SET @cpfun = '".$cpf_fun."'");
  $res4 = $GLOBALS['database']->query("SET @rgfun = '".$rg_fun."'");
  $res5 = $GLOBALS['database']->query("SET @estadofun = '".$estado_fun."'");
  $res6 = $GLOBALS['database']->query("SET @salariofun = '".$salario_fun."'");


  $sql = "CALL CadastrarFuncionario(@nomefun, @cargofun, @nascimentofun, @cpfun, @rgfun, @estadofun, @salariofun);";
  $res7 = $GLOBALS['database']->query($sql);
  if ($res7) {
   aviso("funcionário cadastrado com sucesso!");
  } else {
   aviso("erro!");
  }
 }

 function CadastrarDependente($nome_dep, $nascimento_dep, $funcionario_dep, $parentesco_dep, $conexao){
  $res = mysqli_query($conexao, "CALL CadastrarDependente ('".$nome_dep."', '".$nascimento_dep."', ".$funcionario_dep.", '".$parentesco_dep."')", MYSQLI_USE_RESULT);
  if ($res) {
   aviso("dependente cadastrado com sucesso!");
  } else {
   aviso("erro!");
  }
 }

However, I don't know if the problem is in the code or programming logic, but this procedure that was called is programmed this way:


Tables and Procedures in MySQL:

CREATE TABLE funcionario (
 id INT PRIMARY KEY AUTO_INCREMENT,
 nome VARCHAR(100) NOT NULL,
 cargo VARCHAR(50) NOT NULL,
 nascimento DATE NOT NULL,
 cpf CHAR(20) NOT NULL,
 rg CHAR(20) NOT NULL,
 estado CHAR(50) NOT NULL,
 salario DOUBLE NOT NULL
);

CREATE TABLE dependente (
 id INT PRIMARY KEY AUTO_INCREMENT,
 nome VARCHAR(100) NOT NULL,
 nascimento DATE NOT NULL,
 funcionario_id INT NOT NULL,
 parentesco CHAR(50) NOT NULL,
 FOREIGN KEY (funcionario_id) REFERENCES funcionario (id)
);

# cadastrar

DELIMITER $$
CREATE PROCEDURE CadastrarFuncionario (IN nome_fun VARCHAR(100), IN cargo_fun VARCHAR(50), IN nascimento_fun DATE, IN cpf_fun CHAR(20), IN rg_fun CHAR(20), IN estado_fun CHAR(50), IN salario_fun DOUBLE)
BEGIN
 INSERT INTO funcionario VALUES (null, nome_fun, cargo_fun, nascimento_fun, cpf_fun, rg_fun, estado_fun, salario_fun);
END $$
DELIMITER ;

DELIMITER $$
CREATE PROCEDURE CadastrarDependente (IN nome_dep VARCHAR(100), IN nascimento_dep DATE, IN funcionario_dep INT, IN parentesco_dep CHAR(50))
BEGIN
 INSERT INTO dependente VALUES (null, nome_dep, nascimento_dep, funcionario_dep, parentesco_dep);
END $$
DELIMITER ;

And on the server, PHP is insisting on the same error:


Fatal error: Call to a member function prepare() on a non-object in C:\Users\user\Desktop\USBWebserver v8.6\root\recuperacao - tpi\funcs.php on line 25

Issue: What errors are causing the server to return this warning. And what would be the best practices to create a normal procedure and call it by PHP.

pe.Math
  • 89
  • 1
  • 7
  • The code you've shared does not appear to contain the line with the error `->prepare(...)`. Look at where you are using `prepare` and check the object you're using is actually a valid mysqli resource. I suspect it's failed to connect. – rjdown Oct 25 '19 at 22:35
  • Possible duplicate of [Can I mix MySQL APIs in PHP?](https://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php) – Dharman Oct 25 '19 at 23:14

0 Answers0