Php does not connect to database (Mysql)

Asked

Viewed 2,976 times

2

So, I started recently with php and tried to do a simple crud with php+mysql but, I had a problem that I believe is the connection with mysql. when loading the action (action) into html, the page is empty. Returns no error and inserts it into the database. Follow the script below:

       <form class="central text" name="cadastro" method="post" action="cadastro.php">
        <div>
            <h2>Dados Pessoais</h2>
            <p>Primeiro Nome</br><input type="text" name="primeiroNome"></p>
            <p>Sobrenome</br><input type="text" name="sobrenome"></p>
            <p>Nascimento</br><input type="date" name="nascimento"></p>
            <p>Nacionalidade</br><input type="text" name="nacionalidade"></p>
        </div>
        <input name="salvar" type="submit" value="Salvar">
    </form>

The simple test, just to know if it is connecting. It is without the insertion query in the database, because it does not even pass this connection line:

<?php

$conexao = mysql_connect ("localhost", "root", "") 
echo "Conectado!";
    or die("Não conectado ao banco! Erro: ".mysql_error());
?>

I thank anyone who can help. Just for the record: 1-Yes, to get here and ask someone, I researched a lot! 2-I’m using Linux (LAMP)

1 answer

1


Boss does not usemysql_ no, this function is obsolete... Try this: it has several forms of connection, I use so...

<?php
$servidor = 'localhost';
$usuario = 'root';
$senha = '';
$banco = 'test';
$con = new mysqli($servidor, $usuario, $senha, $banco);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}else{
echo "Conectou !!";
} 
?>

See if it works, anything says we adjust...

  • 1

    It worked! Thank you very much. Everywhere I researched, taught the way I was doing. But anyway, thank you very much!

Browser other questions tagged

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