0

i have a problem with my php code, it gives me the error of : Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given in C:\xampp\htdocs\funcion2.php on line 69

I dont know how to solve it. i need some help pls, i need to finish the project.

<?php
//Conexion a la BBDD
include 'config.php';
session_start();

if($_REQUEST['cur']){$_SESSION['cur2']=$_REQUEST['cur'];}
$cur=$_SESSION['cur2'];
$str=$cur;
$cur=explode ('|', $str);
echo $cur[0];

if ($_REQUEST['alu']){$_SESSION['alu2']=$_REQUEST['alu'];}
$alu=$_SESSION['alu2'];
$str2=$alu;
$alu=explode ('|', $str2);


/*primera consulta*/
$query  = 'select * from curso';
$res=mysqli_query($conexion, $query);

/*segunda consulta*/
$query2 = 'select * from alumnos where cod_curso=$cur[0]';
$res2=mysqli_query($conexion, $query2);



?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
</head>


<body>

<form name="form1" method="post">
<div class="form-group col-md-3">
  
  <label>Curso</label>
  <select name="cur" onchange="this.form.submit() ;">
    <option value="<?php echo $cur[1] ?>" ></option>
    <?php	
     while ($row=mysqli_fetch_array($res))
     {
		 
		 ?>
		 <option value="<?php echo $row[0]."|".$row[1]?>"><?php echo htmlentities($row[1]); ?>
		 </option>
		 
		 <?php } ?>
	
  </select>
   
</div>

<div class="form-group col-md-6">
  <label>Alumnos</label>					
   
   <select name="alu">
   
    <option value="<?php echo $alu[0]?>"</option>
    <?php	
     while ($value=mysqli_fetch_array($res2)) 
     {
		 
		 ?>
		<?php foreach ($alu as $key => $value){ ?>
		 <option value="<?php echo $value[0]."|".$value[1]?>"><?php echo htmlentities($value[1]);?>
		 </option>
		 <?php } ?>
		<?php } ?>
  </select>
    
</div>

	<input type="submit" name="enviar" value="Enviar" hidden />
	
</form>
<?php

echo "Tu curso es: ".$cur[1]."<br/>";
echo "El alumno es: ".$alu[1]."<br/>";
?>

</body>
</html>

There is the problem .The error os mysql

My database have these 2 tables: Table alumnos and Table curso

My tables and the problem

I modified the parameters without @ and the second mysqli_fetch_array.

4
  • 2
    WARNING: Do not suppress errors when calling methods with the @ operator. If something goes wrong you want to know about it and will need to take corrective action, display a useful message for the user, log the problem, or all that and more. It also makes debugging issues like this a whole lot more complicated if you ignore errors that are trying to point out serious problems. Commented May 24, 2017 at 23:32
  • 1
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented May 24, 2017 at 23:32
  • 1
    php.net/manual/en/mysqli-result.fetch-array.php $value=mysqli_fetch_array($conexion, $cur) should be something more like $value=mysqli_fetch_array($res2) as it exppect 1st arg to be results, but i'm not posting this as an answer because both of @tadman comment should be taken into consideration before going any further. Commented May 25, 2017 at 2:20
  • I delete the @ from the $_REQUEST. How can I get the data from the second select, the first select show me the data but the second doesn't work. I'm sorry by my English, I'm from Spain. Commented May 25, 2017 at 8:30

1 Answer 1

1

Why are you sending the connection to the while loop for alumnos?

while ($value=mysqli_fetch_array($conexion, $cur))

It's expecting a DB array result, not the connection.

*******EDIT Now that I've taken a look, you still have errors in this section.

<?php   
    while ($value=mysqli_fetch_array($res2)) 
    {
?>
<?php 
    foreach ($alu as $key => $value){
?>
     <option value="<?php echo $value[0]."|".$value[1]?>">
        <?php echo htmlentities($value[1]);?>
     </option>
     <?php } ?>
    <?php } ?>

Why do you have a for each loop inside a while loop? Unless $value has data that you need to loop inside each row, it is already being looped by the while loop and you would not need the for each.

Sign up to request clarification or add additional context in comments.

4 Comments

I change it for <?php while ($row = mysqli_fetch_array($res2)), but the error persists, it says that mysqli_fetch_array expects 1 parameter to be mysqli_result. Sorry I'm newbie at php, I have practiced only since 2 weeks
I put a foreach because sql says that that tipe of data in alumnos.nombre can´t be a string so i have to do a foreach no?
Well I'm not sure how your data is structured but you $value in the while loop and then you're looping $alu in the foreach and over writing the original $value variable. Let me take look at your structure and I'll update my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.