trying to connect with mysqli

Asked

Viewed 102 times

0

I’m trying to connect to the database via myqli with php but I’m not getting it. Follow the code:

<html>

<head>
  <title>sistema de cadastro</title>
</head>

<body>
  <form method="get" action="cadastramento.php">
    nome:
    <input type="text" name="name" />
    <p></p>
    sobre nome:
    <input type="text" name="sobrenome" />
    <p></p>
    senha:
    <input type="password" name="senha" />
    <p></p>
    <input type="submit" value="" />
  </form>

</body>

</html>

Now the connection code mysqli in php:

<html>
<head>
<title> cadastramento</title>
</head>
<body>

<?php
$servidor = 'localhost';
$usuario = 'root';
$senha = '';
$banco = 'mydb';

$mysqli = new mysqli($servidor, $nome, $ssobrenome, $senha);

if (mysqli_connect_errno()) trigger_error(mysqli_connect_error());

?>

<?php

$mysqli = new mysqli('localhost', 'root', '', 'mydb');

$sql = "SELECT `id`, `titulo` FROM `cadastramento` LIMIT 5";
$query = $mysqli->query($sql);
while ($dados = $query->mysqli_fetch_array()) {
  echo 'ID: ' . $dados['id'] . '';
  echo 'Título: ' . $dados['titulo'] . '';
}
echo 'Registros encontrados: ' . $query->num_rows;

?>

  • Define your question better, help the answers, at first I understood about connecting the bank. and follow answer.

  • You need to provide more details: some error occurs or nothing happens?

  • You will not pass a password by get The.o

2 answers

2

You are connecting twice and the first is reporting undeclared variables in the code. Change and leave this way:

<?php

$servidor = 'localhost';
$usuario = 'root';
$senha = '';
$banco = 'mydb';

$mysqli = new mysqli($servidor, $usuario, $senha, $banco);

if (mysqli_connect_errno()) {
    trigger_error(mysqli_connect_error());
} else {

    $sql = "SELECT `id`, `titulo` FROM `cadastramento` LIMIT 5";
    $query = $mysqli->query($sql);
    while ($dados = $query->mysqli_fetch_array()) {
        echo 'ID: ' . $dados['id'] . '';
        echo 'Título: ' . $dados['titulo'] . '';
    }
    echo 'Registros encontrados: ' . $query->num_rows;
}
?>

0

You need to pass the data via post:

And the button:

<input type="submit" value="Enviar"/>

I recommend doing it step by step. For example:

check if you connected to the database server. Then check whether you connected to a particular database passed in the parameter.

If you do everything, to correct the mistakes is more complex. Step by step, especially when we have no experience, it is better to solve each bug.

Browser other questions tagged

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