Last active
July 16, 2020 19:40
-
-
Save mikelemus27/76000db137bce7f9658bf560282bf6c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// this procedures must exts in db | |
//======================================== | |
// Drop procedure if exists ProcSuma; | |
// DELIMITER // | |
// CREATE PROCEDURE ProcSuma(IN nm1 INT, IN nm2 INT, OUT value INT) | |
// BEGIN | |
// SET value = nm1+nm2; | |
// END // | |
// delimiter ; | |
//example of correct call to procedures| | |
// set @res=0; | |
// call ProcSuma(2,3,@res); | |
// select @res as resultado; | |
// ======================================== | |
// manejo de saltos de line si es en terminal o en web | |
// ======================================== | |
$in_terminal=TRUE; | |
// if ($in_terminal){$ln="\n";}else{$ln="</br>";} | |
$in_terminal?$ln="\n":$ln="</br>"; | |
// ======================================== | |
//======Datos de conexion======================== | |
echo "iniciando la conexion $ln"; | |
$driver = 'mysql'; | |
$database = "dbname=databasename"; | |
$dsn = "$driver:host=localhost;$database"; | |
$username = 'root';// superusuario | |
$password = ''; | |
//================================================ | |
/// intentando Conexion A BD | |
try { | |
$conn = new PDO($dsn, $username, $password);//cadena de conexion | |
echo "Database ".$database." Connected$ln"; | |
}catch(PDOException $e){ | |
echo "la exception es ". $e->getMessage(); | |
} | |
if ($conn){ | |
echo "conexion exitosa $ln"; | |
}else | |
{ echo "conexion fallida $ln"; | |
} | |
//=============Set global Database variable *================================= | |
try { | |
echo "Creando la variable @res$ln"; | |
// select all users | |
$sql = 'Set @res = 3;'; | |
$stmt = $conn->prepare($sql); | |
$stmt->execute(); | |
echo "select ejecutado\n $ln"; | |
} catch(PDOException $e) { | |
echo $sql . "<br>" . $e->getMessage(); | |
} | |
//=============Call Procedure*================================= | |
try { | |
echo "LLamando al procedimiento ProcSuma (3,6,@res) $ln"; | |
// select all users | |
$sql = 'call ProcSuma(3,6,@res)'; | |
$stmt = $conn->prepare($sql); | |
$stmt->execute(); | |
echo "select ejecutado\n $ln"; | |
} catch(PDOException $e) { | |
echo $sql . "<br>" . $e->getMessage(); | |
} | |
//=============Obteniendo el valor de @res*================================= | |
try { | |
echo "Haciendo Select @res$ln"; | |
// select all users | |
$sql = 'SELECT @res;'; | |
$stmt = $conn->prepare($sql); | |
$stmt->execute(); | |
echo "select ejecutado$ln"; | |
} catch(PDOException $e) { | |
echo $sql . "$ln" . $e->getMessage(); | |
} | |
if ($stmt->rowCount() > 0) { | |
//Row has been found | |
//Do ...... | |
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){ | |
echo "si hay ".count($row)."filas $ln"; | |
foreach($row as $value) | |
{ echo"el valor de @res es: ". $row["@res"]."$ln"; | |
echo "el valor de @res es: ".$value[0]."$ln"; | |
echo sprintf("el valor de @res es: %s $ln ", $value); | |
} | |
echo "$ln"; | |
} | |
} else { | |
//No row found | |
//Do.... | |
echo "consulta sin resultados"; | |
echo "\n $ln"; | |
} | |
//======================================================= | |
echo "finalizando la ejecucion $ln"; | |
?> | |
"localhost.localdomain" 19:39 16-Jul-20 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment