Error connecting: access denied for the user '' @ 'localhost'

1

I am receiving data from some variables by means of url and I want to insert those same values into a MySQL database, but I'm getting an error:

  

Warning: mysql_query (): Access denied for user '' @ 'localhost' (using password: NO) in C: \ xampp \ htdocs \ Medical \ views \ adddoc.php on line 17

     

Warning: mysql_query (): A link to the server could not be established in C: \ xampp \ htdocs \ Medical \ views \ adddoc.php on line 17 "

My code is as follows:

<?php
include "conexionBD.php";
$idpaciente=$_GET["cp"];
$iddoctor=$_GET["cd"];
$estado='1';
$consulta="select id_paciente,id_doctor,status from relaciones";
$encontrado=0;
$resultado=$bd->consultar($consulta);
    foreach ($resultado as $row){
        if($row["id_paciente"]==$idpaciente && $row["id_doctor"]==$iddoctor && $row["status"]=='1'){
            $encontrado=1;
            $respuesta="El doctor seleccionado ya es su doctor";
    }
}

if($encontrado==0){
        mysql_query("insert into relaciones (id_paciente,id_doctor,status) values ('$idpaciente','$iddoctor','$estado')");
        $respuesta="Doctor agregado correctamente";
    }
?>
    
asked by David I. Gutierrez 02.04.2018 в 01:16
source

2 answers

2

I mentioned the connections to database managers require:

  

I tell you to check that you write correctly the data or   access credentials respecting lowercase, if you change it even if   a character will no longer be valid

  • Server (if it is your own pc it is localhost, if it is remote it places the IP of the server)
  • User
  • Password ( optional, but check if you need it to sign in )
  • name of the database
  • Here are two examples, which depend on which connection driver you use ie mysqli or pdo

    MYSQLI

    $conexion = new mysqli("localhost", "root", "contrasenia", "basedatos");
    

    PDO

    $conecta = new PDO("mysql:host=localhost;dbname=database","root", "contrasenia");
    
      

    Since as you can notice the error tells you that the connection could not be   established and also mentions denied access to the user, why not   you used the access password

        
    answered by 02.04.2018 в 01:29
    2

    Thanks for the comments, I was already using a password so I miss the error message. I solved it in the following way.

    <?php
    include "conexionBD.php";
    $idpaciente=$_GET["cp"];
    $iddoctor=$_GET["cd"];
    $estado='1';
    $consulta="select id_paciente,id_doctor,status from relaciones";
    $encontrado=0;
    $resultado=$bd->consultar($consulta);
    	foreach ($resultado as $row){
    		if($row["id_paciente"]==$idpaciente && $row["id_doctor"]==$iddoctor && $row["status"]=='1'){
    			$encontrado=1;
    			$respuesta="El doctor seleccionado ya es su doctor";
    	}
    }
    
    if($encontrado==0){
    	$insertar_relacion="insert into relaciones (id_paciente,id_doctor,status) values ('$idpaciente','$iddoctor','$estado')";
    	$res=$result=$bd->insertar($insertar_relacion);
    		$respuesta="Doctor agregado correctamente";
    	}
    ?>

    changing the way of inserting the data since the one used was old mysql so to speak it was combining it with PDO, that's why the error.

        
    answered by 02.04.2018 в 01:45