1

this is probably a very simple question to solve, however I've been stuck with this for a while and I can't figure out for the life of me what's wrong with my code. It may be just a syntax mistake but I've gathered this code from other questions and it should work but I keep getting the errors : - Undefined variable : mysqli and - Call to a member function query() of a non-object , both in the line "$result = $mysqli->query($sql);".

Here's the snippet of my code where I have the dropdown menu set up.

<label class="control-label" for="formInput85">Professor</label>
                                           
<?php

$sql = "SELECT name FROM professores";
$result = $mysqli->query($sql);

echo "<select class=".'"form-control"'.">";
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";

?>

And here's my code in the very beginning of the page, that is connecting to my database in phpmyadmin.

<?php
 session_start();
 echo $_SESSION['name'];
 
 $servername = "localhost"; 
 $username = "root"; 
 $password = ""; 
 $dbname = "teste"; 

 $conn = new mysqli($servername,$username,$password,$dbname); 
?>

Thank you for your help! If you have any tips on how to send the value selected into another row of the table I gladly appreciate it as it will be my next step :)

Joe
  • 21
  • 3

1 Answers1

1

$mysqli is not the right variable in this case. So this line:

$result = $mysqli->query($sql);

has to become

$result = $conn->query($sql);
pavlovich
  • 1,935
  • 15
  • 20
  • 1
    Thank you!!! Exactly what I needed :P I didn't realize mysqli was the connection variable, oh well the more you know :) Any tips on sending the selected value into a table? – Joe Nov 28 '15 at 15:55
  • @Joe you're welcome ;) – pavlovich Nov 28 '15 at 15:56