Database connection error

Asked

Viewed 975 times

7

I’m having a problem connecting to Mysql database with PHP I’m using WAMP created a database in my Phpmyadmin made my connection via PHP but this error appears when I click to send in my form:

SS

These are the codes of my connection:

config.php

<?php
    $db['server']   ='localhost';
    $db['user']     ='felipe';
    $db['password'] ='lalala';
    $db['dbname']   ='base_teste';

    //estabelece uma conexao com server de bd ('servidor', 'usuario', 'senha')
    $conn = mysql_connect($db['server'],$db['user'],$db['password']);

    //conexao com banco
    mysql_select_db($db['dbname'],$conn);
?>

php functions.

<?php
    function adicionar($nome,$idade){
        $sql = "insert into tb_alunos (nome,idade) values ('$nome','$idade')";
        mysql_query($sql);
    }
?>

adds.php

<?php
    include 'config.php';
    include 'funcoes.php';

    adicionar ($_REQUEST['nome'],$_REQUEST['idade']);
?>

index php.

<!DOCTYPE html>
<html>
<head>
    <title>conexao teste</title>
</head>
<body>
    <form action="adiciona.php" method="post">
        nome <input type="text" name="nome">
        Idade <input type="text" name="idade">
        <input type="submit" value="Enviar">
    </form>
</body>

2 answers

6


  • what I saw and only change mysli to mysqli

  • I changed here and it didn’t work even so I’m very inexperienced in php my first connection to database because then I will etr that log to another application already ready I’m studying you could make me a simulation of how the code would look

  • 3

    Yeah, it’s not just changing a letter, you have to study the documentation, see the examples, read the switch I’ve already made to another user. There is no magic in programming. There is effort and learning. Here is not a place to ask people to do for you, here we just clarify doubts. As a user has already asked to show how does the conversion, any other request in this sense would be considered duplicate.

  • Why don’t you advise using the PDO? With it you can use the same code for multiple databases, and still have Prepared statement, which makes it difficult to carry out the SQL Injection attack on your application.

  • @Andrera Msqli also has it. And why do you want to use multiple databases? Unless you’re making one of these generic software for other people to customize, it’s virtually impossible to need another database. Find out about the disadvantages of PDO to advantage (debatable) of it.

  • @BIGOWN I find it advantageous to give freedom to the customer to change the SGBD to one of their preference without worrying about the code already written. The only disadvantage I find when using PDO, is its performance compared to Mysqli, which is not so glaring.

  • But there are others. You can have whatever opinion you want about this to be advantageous or not, but the simple fact that you say this is good without having a concrete case is choosing the tool before you see what the problem is. Anyway, this is no place for debate.

  • @Bigown Actually, it depends on the application in which the friend is developing. If it is something that does not require so much of the server and does not need a high performance, I do not see why not use the PDO. I am sorry if I made a bad impression on my previous comments, my aim was not to point out which is best, but to point out which seemed to me most appropriate for the situation of the author of the question.

  • @Andrera I understand, don’t worry. But it’s the other way around. If you don’t need the one thing he does good, you don’t have to use it. Using unused, using because someone told you to use it, because everyone uses it without knowing why they’re using it, it’s not plausible. I will close because we are leaving the purpose of the site. And there are several reasons not to use the PDO, but neither is the focus of this question. If you want to do one on this.

Show 4 more comments

3

Browser other questions tagged

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