I have this php function:
function getCredEstud(int $ciEstud) {
include_once 'conexion.php';
$result = mysqli_query($conexion,"SELECT DISTINCT `creditosEstudiante` FROM `op_JOIN_est-insc-mat` WHERE ciEstudiante = '".$ciEstud."'");
$i=0;
while($row = mysqli_fetch_array($result)) {
return (int)$row["creditosEstudiante"];
$i++;
}
mysqli_close($conexion);
}
which should get the value of "creditosEstudiantes" from a given "ciEstudiante" of the below table.
The problem is that it return nothing.
Could someone tell me what i am doing wrong?
Thanks in advance! =)
randomNum ciEstudiante creditosEstudiante codigoMateria tipoMateria cupoMateria creditosMateria
0 1780859 75 1035 CURRICULAR 0 3
0 1780859 75 1954 OPTATIVA 25 0
0 1780859 75 1025 CURRICULAR 0 3
0 1780859 75 1910 OPTATIVA LIBRE 0
0 1780859 75 1948 OPTATIVA 60 0
0 1780859 75 2105P CURRICULAR 0 4
0 1780859 75 2100P CURRICULAR 0 4
0 1780859 75 2095 CURRICULAR 0 3
0 1780859 75 4170 CURRICULAR 0 3
0 1780859 75 1932 OPTATIVA 30 0
0 1780859 75 3140 CURRICULAR 0 4
0 1780859 75 2190 CURRICULAR 0 4
0 1780859 75 2115P CURRICULAR 0 4
0 1921177 93 2115P CURRICULAR 0 4
0 1921177 93 2095 CURRICULAR 0 3
0 1921177 93 2105P CURRICULAR 0 4
0 1921177 93 2100P CURRICULAR 0 4
0 2020703 269 3140 CURRICULAR 0 4
0 2020703 269 4160 CURRICULAR 0 3
0 2762533 249 3020P CURRICULAR 0 28
0 2971526 355 1954 OPTATIVA 25 0
0 3102055 55 1021 CURRICULAR 0 25
0 3102055 55 2095 CURRICULAR 0 3
Here is the full code:
<?php
function getRandomCIEstud() {
include_once 'conexion.php';
$result = mysqli_query($conexion,"SELECT DISTINCT `ciEstudiante` FROM `op_JOIN_est-insc-mat` ORDER BY RAND() LIMIT 1");
$i=0;
while($row = mysqli_fetch_array($result)) {
//$selectedCI=$row["ciEstudiante"];
return $RandomCIEstud=$row["ciEstudiante"];
//echo $RandomCIEstud=(int)$row["ciEstudiante"];
$i++;
}
mysqli_close($conexion);
}
function getCredEstud(int $ciEstud) {
include_once 'conexion.php';
$where = 'WHERE `ciEstudiante` = ?';
if (empty($ciEstud)) {
$ciEstud = 0;
$where = 'ORDER BY RAND() LIMIT 1';
}
$sql = "SELECT DISTINCT `creditosEstudiante` FROM `op_JOIN_est-insc-mat` ".$where;
//result is boolean for query other than SELECT, SHOW, DESCRIBE and EXPLAIN
if ($stmt = mysqli_prepare($conexion, $sql)) {
// bind parameters for markers
if ($ciEstud <> 0) {
//mysqli_stmt_bind_param($stmt, "s", $ciEstud);
mysqli_stmt_bind_param($stmt, "i", $ciEstud);
}
// execute query
mysqli_stmt_execute($stmt);
// bind result variables
mysqli_stmt_bind_result($stmt, $result);
// fetch values
$i=0;
while (mysqli_stmt_fetch($stmt)) {
return (int)$result;
$i++;
}
if ($i==0) {
// if no entry is found return -1
return -1;
}
// close statement
mysqli_stmt_close($stmt);
} else {
printf("Error: %s.\n", mysqli_stmt_error($stmt));
}
mysqli_close($conexion);
}
?>
<!DOCTYPE html>
<html>
<head>
<title> Retrive data</title>
</head>
<body>
<table>
<tr>
<td>getRandomCIEstud(): <?php $randomCI=getRandomCIEstud(); echo $randomCI; ?></td>
</tr>
</table>
<table>
<tr>
<td>getCredEstud(): <?php echo getCredEstud((int)$randomCI); ?></td>
</tr>
</table>
</body>
</html>
This is the code I have so far. I tried everithing but it doesnt work right now. It doesnt show any error, it just dont return anything. I guess It is something wrong with the way I am passing the parameters.
Thank you all very much for your help