Print data from the MySQL table in PHP

1

I have the following code:

<?php
//iniciando a conexão com o banco de dados 
include_once("conectar.php");
if (!$strcon) {
 die('Não foi possível conectar ao Banco de Dados');
}

$sql = mysqli_query("SELECT * FROM cadastro");
$exibe = mysqli_fetch_assoc($sql);
echo "<table>"; 
echo  "<tr><td>Nome:</td>";
echo "<td>".$exibe["Nome"]."</td></tr>";
?>

I have tried to create the structure in several ways. The results displayed in HTML for me are:

  

Warning: mysqli_query () expects at least 2 parameters, 1 given in C: \ Program Files \ EasyPHP-Devserver-17 \ eds-www \ Sites \ Projects \ Schedule \ query.php on line 18

     

Warning: mysqli_fetch_assoc () expects parameter 1 to be mysqli_result, null given in C: \ Program Files \ EasyPHP-Devserver-17 \ eds-www \ Sites \ Projects \ Schedule \ query.php on line 19   Name:

My line 18:

$sql = mysqli_query("SELECT * FROM cadastro");

My Line 19:

$exibe = mysqli_fetch_assoc($sql);

I have already modified the parameters in several ways, but I do not find the exact problem.

    
asked by anonymous 24.08.2018 / 15:48

1 answer

4

You are only passing a parameter to the mysqli_query () function that is the query itself. Being the correct pass a connection variable followed by the query:

$conexao= mysqli_connect("localhost", "my_user", "my_password", "world");
$sql = mysqli_query($conexao, "SELECT * FROM cadastro");

Probably this connection variable should already be being built in your "connect.php" file being $strcon , which you at the beginning of the script to validate the connection, only being necessary to pass it as the first parameter in the function, getting like this:

$sql = mysqli_query($strcon, "SELECT * FROM cadastro");

Retrieved from official documentation here

    
24.08.2018 / 15:55