I’m having this error "Warning: mysqli_query() expects Parameter 1 to be mysqli" in php when looking up the database

Asked

Viewed 1,130 times

-1

Hello I’m trying to search the courses from the database.To see the courses already registered not to register in db again. I’m showing this error:

  Warning: mysqli_query() expects parameter 1 to be mysqli, null given in 
  /storage/emulated/0/Documents/sistemaescolar/public/cad/curso.php on line 6

the connection code

$host="127.0.0.1";
$db="sistemaescolar";
$pswd="";
$user="root";
$connect  =  mysqli_connect ($host,$user,$pswd,$db);

if (mysqli_connect_errno()){
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
} 


I made this page below to decide what kind will get cadre,:

include_once "class_pessoa.php";
if(isset($_POST["buttonSubmit"])){
    $escolha = filter_input(INPUT_POST,'opselect',FILTER_SANITIZE_NUMBER_INT);
    if(filter_var($escolha,FILTER_VALIDATE_INT)){
        include_once "cad/conexao.php";
        $sql = "SELECT * from tipoCad where id=$escolha";
        $resultado = mysqli_query($connect,$sql);
        $dado= mysqli_fetch_assoc($resultado);
        if($dado["nome"] =="Curso"){
            $curso = new Curso($dado["nome"]);
        }
        mysqli_close($connect);
    }                           
}

In the part where you have the class Course,


Below is the code that is giving error when searching the bank:

    $sql = "select * from cursos";
    $resultado=mysqli_query($connect,$sql); 
    while($dados= mysqli_fetch_assoc($resultado)){
        echo $dados['nome'];
    }

  • https://answall.com/questions/101172

  • $sql = "SELECT * from tipoCad where id=$escolha"; puts $escolha in single quotes $sql = "SELECT * from tipoCad where id='$escolha'";

1 answer

4


If you give a echo in that SELECT statement

$escolha="qqcoisa";
$sql = "SELECT * from tipoCad where id=$escolha";
echo $sql;

the return will be SELECT * from tipoCad where id=qqcoisa

and this SQL results in error Unknown column 'qqcoisa' in 'where clause'

translating Coluna desconhecida 'qqcoisa' em 'where cláusula'

The correct is to put the variable $escolha in single quotes

$escolha="qqcoisa";
$sql = "SELECT * from tipoCad where id='$escolha'";
echo $sql;

so that the syntax is correct, that is

 $sql = "SELECT * from tipoCad where id='$escolha'";

sql needs to use ' (single quotes) when it’s string, for numbers you don’t need

$sql = "SELECT * from tipoCad where id=10";

If there is a column named qqcoisa in the SELECT * from tipoCad where id=qqcoisa will return records whose column values id are equal to the values in the column qqcoisa

Browser other questions tagged

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