trying to connect with mysqli

0

I'm trying to connect to the database via myqli with php but I can not. Here is 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 mysqli connection code 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;

?>

    
asked by anonymous 09.09.2015 / 23:52

2 answers

2

You are making the connection 2 times and the first one is reporting variables not declared in the code. Change and leave it 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;
}
?>
    
10.09.2015 / 12:59
0

You need to pass the data via post:

And the button:

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

I recommend doing step by step. For example:

Verify that you have connected to the bank server. Then check if you have connected to a certain bank passed in the parameter.

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

    
10.09.2015 / 14:05