0

I tried writing my own function for my final project, but I always get this:

Warning: mysql_real_escape_string(): Access denied for user ''@'localhost' (using password: NO) in /home/ubuntu/workspace/final/includes/config.php on line 50

Warning: mysqli_connect(): (HY000/1045): Access denied for user ''@'localhost' (using password: NO) in /home/ubuntu/workspace/final/includes/config.php on line 3

This is my function:

function queryDB($q)
{
    $conn = mysqli_connect("localhost", $username, $password, $dbname);
    if (mysqli_connect_errno()) 
    {
        die("Connection failed. " . mysqli_connect_error());
    } 
    $result = mysqli_query($conn, $q);
    mysqli_close($con);
    return $result;
}

1 Answer 1

1

$username is blank as per the error message: Access denied for user ''@'localhost'..... . There is nothing between the two ticks (single quotes) after user. That makes $password, $dbname suspect as well. Also, check the variable name used in your close statement.

1
  • Thanks. I forgot to add this line at the beginning of the function: global $username, $password, $dbname;
    – Alex
    Jun 8, 2016 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .