Access Denied mysql

0

This error appears: Warning: mysqli_connect () [function.mysqli-connect]: (28000/1045): Access denied for user 'root' @ 'localhost' (using password: YES)

This is the code:

?php 

$server = "localhost";

$user = "root";

$pass = "";

$db = "ohvee";


$conect = mysqli_connect("$server","$user","pass","db")

    or die("ERROR");

$name = $_POST['name'];
$content = $_POST['content'];

$insertar = "INSERT into wall values ('$name','$content')";

$resultado = mysql_query($conect , $insertar)
    or die("ERROR AL INSERTAR");

mysql_close($conect);

echo "Datos insertados correctamente";

?>

WHAT DID I DO EVIL?

    
asked by Flavio C. Siria 25.06.2017 в 00:09
source

3 answers

1

Apart from the fact that I give you an answer to your original question, I will make other observations about your code, the first error is that you are not entering the password when your server clearly tells you that it requires it

On the other hand, the second error is that you are working with the mysqli driver first and then mixing with the mysql driver that is already in disuse then look at the changes that I have made to your work.

?php 

$server = "localhost";

$user = "root";

$pass = "algunpass";

$db = "ohvee";


$conect = mysqli_connect("$server","$user","pass","db")

    or die("ERROR");

$name = $_POST['name'];
$content = $_POST['content'];

$insertar = "INSERT into wall values ('$name','$content')";

$resultado = mysqli_query($conect , $insertar)
    or die("ERROR AL INSERTAR");

mysqli_close($conect);

echo "Datos insertados correctamente";

On the other hand I tell you that your next problem is that just as this code is sensitive to SQL INJECTION, therefore you should make the following changes

<?php 

$server = "localhost";

$user = "root";

$pass = "";

$db = "ohvee";


$conect = mysqli_connect("$server","$user","pass","db")

    or die("ERROR");

$name = $_POST['name'];
$content = $_POST['content'];

$insertar = $conect->prepare("INSERT into wall(name, content) values (?,?)");
$insertar->bind_param('s', $name);
$insertar->bind_param('s', $content);
$insertar->execute();


$resultado = mysql_query($conect , $insertar)
    or die("ERROR AL INSERTAR");

mysql_close($conect);

echo "Datos insertados correctamente";
  • As notes above I am with interogation signs using placeholders
  • I use the prepare method
  • I use bind_param to set what kind of value I'm going to send: s is for strings, i is for integer, d is for double and b is for blob data
  • Finally, finally, access to the execute method so that my query is processed and can be carried out
  • answered by 19.05.2018 / 14:36
    source
    0

    Hello, you have several errors like for example you do not pass the password to the connection, although it is not necessary because you are doing the project in localhost, also I recommend you to use Object Oriented Programming in php.

    Here is a commented example:

    class Db {
        // The database connection
        protected static $connection;
    
        /**
         * Connect to the database
         * 
         * @return bool false on failure / mysqli MySQLi object instance on success
         */
        public function connect() {    
    // Carga la configuración como una matriz. Utilice la ubicación real de su archivo de configuración
            if(!isset(self::$connection)) {
    // Carga la configuración como una matriz. Utilice la ubicación real de su archivo de configuración
                $config = parse_ini_file('./config.ini'); 
                self::$connection = new mysqli('localhost',$config['username'],$config['password'],$config['dbname']);
            }
    
    // Si la conexión no tuvo éxito, maneje el error            if(self::$connection === false) {
    // Maneja el error: notifica al administrador, registra en un archivo, muestra una pantalla de error, etc.
                return false;
            }
            return self::$connection;
        }
    
        /**
    * Consultar la base de datos
        *
              * @param $ query La cadena de consulta
              * @return mixed El resultado de la función mysqli :: query ()
              * /
        public function query($query) {
            // Conexion a la base de datos.
            $connection = $this -> connect();
    
    // Consultar la base de datos
            $result = $connection -> query($query);
    
            return $result;
        }
    
        /**
         * Obtener filas de la base de datos (consulta SELECT)
              *
              * @param $ query La cadena de consulta
              * @return bool Falso en falla / matriz Filas de base de datos en el éxito
              * /
        public function select($query) {
            $rows = array();
            $result = $this -> query($query);
            if($result === false) {
                return false;
            }
            while ($row = $result -> fetch_assoc()) {
                $rows[] = $row;
            }
            return $rows;
        }
    
            /** Obtener el último error de la base de datos
              *
              * @return string Mensaje de error de la base de datos
              * /
        public function error() {
            $connection = $this -> connect();
            return $connection -> error;
        }
    
        /**
     Valor de referencia y escape para uso en una consulta de base de datos
              *
              * @param string $ value El valor a ser citado y escapado
              * @return string La cadena citada y escapada
              * /
        public function quote($value) {
            $connection = $this -> connect();
            return "'" . $connection -> real_escape_string($value) . "'";
        }
    }
        
    answered by 25.06.2017 в 00:27
    0

    I'm doing my project on localhost too and using a connection.php

    ?php
    // datos para la conexion a mysql
    define('DB_SERVER','localhost');
    define('DB_NAME','ohvee');
    define('DB_USER','root');
    define('DB_PASS','');
    $con = mysqli_connect(DB_SERVER,DB_USER,DB_PASS);
    mysqli_select_db($con, DB_NAME);
    ?>

    And then in the php that you are going to use you use:

    <?php
    session_start();
    include_once "conexion.php";

    It has worked perfectly for me, too, without a password. Greetings.

        
    answered by 19.05.2018 в 13:41