Print Mysql Table data in PHP

Asked

Viewed 1,135 times

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 various 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 Calendar 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 Calendar query.php on line 19 Name:

My line 18:

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

My Line 19:

$exibe = mysqli_fetch_assoc($sql);

I’ve modified the parameters in several ways, but I can’t find the exact problem.

1 answer

3


You are only passing a parameter to the function mysqli_query() which is the query itself. The correct being to 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 is already being built in your "connect.php" file being the $strcon, you at the beginning of the script to validate the connection, only being necessary to pass it as the first parameter in the function, thus:

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

Taken from official documentation here

  • Probably, it should only take this variable that is being built in this "connect.php" and pass as the first parameter.

  • I added your comment to the reply @Leocaracciolo.

  • 1

    Now it’s gone off!

  • 1

    the connection variable is $strcon

Browser other questions tagged

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