Inserting a PROCEDURE in query() in PHP

Asked

Viewed 176 times

-1

Guys, I’ve been trying for a while to integrate a project together with PHP, but when calling it, PHP keeps showing 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 do not know if the problem is in the code or in the logic of programming, but this process 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:


inserir a descrição da imagem aqui


Issue: What(s) are the errors that are causing the server to return this warning. And what (is) would be the best practices to create a normal process and call it by PHP

  • Please place codes as text instead of images.

  • @Cypherpotato *

1 answer

1


Not being made the correct bind of the parameters, can do so:

// declara o sql
$sql = "CALL CadastrarFuncionario(@nomefun, @cargofun, @nascimentofun, @cpfun, @rgfun, @estadofun, @salariofun);";

// prepara a execucao da procedure
$res7 = $GLOBALS['database']->prepare($sql);

// passa os parametros
$res7->bindParam($nome_fun, $cargo_fun, $nascimento_fun, $cpf_fun, $rg_fun, $estado_fun, $salario_fun);

// executa
$res7->execute();
  • i tried to run these commands in the database and gave the following error: Fatal error: Call to a Member Function prepare() on a non-object in C: Desktop users Usbwebserver V8.6 root recovery - tpi funcs.php on line 25

  • I usually use these Object Orientation commands when I use the PDO() class, which has a set of features and methods that do these services. In this case, I am trying to register the employee in the simplest way possible, but so far I cannot understand how PHP handles Mysql procedures and how I can use best practices to call a process correctly in this function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.