Link to home
Start Free TrialLog in
Avatar of Overthere
Overthere

asked on

MySql , PHP code and Access denied for user ''@'localhost' (using password: NO)

Hi Folks,

     I using PHP, Windows 7 Professional and developing using WebMatrix.
I installed MySQL 5.6 with MySQL Workbench 6.3 CE.
When I try to insert a record into the table in MySQL via PHP code, I receive:

Access denied for user ''@'localhost' (using password: NO)  

It makes no sense to me since I have included configuration files for setting up the db name, user etc and establishing a db connection. They are  include files and are in my page right at the top.
I am able to use MySQL via the Workbench just fine for deleting, inserting etc. and had the user name (default of root) and my password stored in the vault which  have administrator rights. They are the same as I entered when installing MySQL. I stopped and started MySQL via services. No change. Same error when I executed my page.
I am able to execute queries via PHP code, but the insert gives me that error.
I understand  users can be prohibited from certain functions such as inserting but I am logged in as administrator.
I have checked that I do have correct nbr and type of variables and they check out okay.
Below is the coding for the PHP Page:

<?php
    require_once ("Includes/simplecms-config.php");
    require_once ("Includes/connectDB.php");
    include("Includes/session.php");
$baderr= "";
$verrswi = "";

if (isset($_POST['submit']))
    {
         
        // intialize switches
        $baderr= "";
        $verrswi = "";

   
       $country = $_POST['country'];
         
       $travel = $_POST['travel'];
   

       $rating = $_POST['rating'];
     
       $brands = $_POST['brands'];
       
       $supplerid = $_SESSION["SupplierRecId"];
   
      $sqlsupplierid = $_SESSION["MsSqlSupplierId"];
           


      //check to make sure they have chosen at least one tag
     // if ($country == 0 and $travel == 0 and $rating == 0 and $brands == 0)
      //    {  
        //       $baderr = "YOU MUST SELECT AT ATLEAST ONE TAG FOR SEARCHING";
        //  }

           // if they have chosen an option then insert record
               $sql = "INSERT INTO camplibrary(SupplierRecId,MsSqlSupplierId,CountryRecId,BrandRecId,RatingRecId,TravelRecId)
               values($supplerid,$sqlsupplierid,$country,$brands,$rating,$travel)";
                mysql_select_db('myworkdb');
                $retval = mysql_query($sql, $databaseConnection);
                if (!$retval)
                    {
                     echo mysql_error();
                }else{
                     $baderr = "SUCCESS - RECORD SAVED";
                    echo "<h7 style='color:'red';alignment-adjust: 'middle'>$baderr</h7>";
                      mysqli_close($databaseConnection);
                    }
          //  }
       
                 
               
    } else {
           // $baderr = "Username/password combination is incorrect. Please try again";
           // echo "<h7 style='color:'red';alignment-adjust: 'middle'>$baderr</h7>";
    }
?>
followed by a bunch of html.

This is the  include file that defines the db etc ( simplecms-config.php)
The DB_PASSWORD and DEFAULT_ADMIN_PASWORD are the same.
THE DB_USER and DB_PASSWORD match what I entered doing the installation of MySQL.
 
<?php
    define('DB_NAME', 'myworkdb');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'mypassword');
    define('DB_HOST', 'localhost');

    define('DEFAULT_ADMIN_USERNAME', 'admin');
    define('DEFAULT_ADMIN_PASSWORD', 'mypasword');
?>

This is the database connection include file:
<?php
    require_once ("/Includes/simplecms-config.php");

    // Create database connection
    $databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($databaseConnection->connect_error)
    {
        die("Database selection failed: " . $databaseConnection->connect_error);
    }

 
?>
t
Is the problem caused by using mysqli_ and mixing mysql_  together?
I understand that MySQL_ is a historical API and mysqli_ is a newer version that is object oriented.
And that PDO_MySql is the PDO introduced into PHP to make migration across all db  easier without changing code.
I have made sure the variables have values and inserting into right order etc but I do not even get that far since I am denied access.
Thanks for any help
Avatar of F P
F P
Flag of United States of America image

The error is with your include on the require_once. You're pointing to the root instead of a relative root. I don't know what your document structure is, but that's your problem. Change this code:

<?php
    require_once ("/Includes/simplecms-config.php");

    // Create database connection
    $databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($databaseConnection->connect_error)
    {
        die("Database selection failed: " . $databaseConnection->connect_error);
    }

Open in new window


to this:

<?php
    // it seems your config file and db connect are in the same directory from the require once at the top of your code first posted...
    require_once (dirname(__FILE__) . "/simplecms-config.php");

    // Create database connection
    $databaseConnection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if ($databaseConnection->connect_error)
    {
        die("Database selection failed: " . $databaseConnection->connect_error);
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of F P
F P
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If that doesn't solve the problem, it's because the user that the Apache or IIS, whichever you're using,  is running doesn't have permission to access the files correctly. Make sure the user running the web server application has at least read and execute to not just the files, but the folder structure leading up to it with at least read. If it can't read the parent folder contents.... no go.
Avatar of Overthere
Overthere

ASKER

Good advise. I will check into the articles. I was able to resolve the problem - at least for now. I realized I had mixed MySQL_ and mysqli. Once I replaced the MySQL_ with mysqli_ commands, it gave me an error relating to my syntax construction of the insert statement...
That'll do it too... You should also know that if you had a prior connection with anything in that extension, it would've reopened that connection and used it as a resource.
<?php

 $country = (int) $_POST['country'];
       $travel = (int) $_POST['travel'];
       $rating = (int) $_POST['rating'];
       $brands = (int) $_POST['brands'];
       $supplerid = (int) $_SESSION["SupplierRecId"];
      $sqlsupplierid = (int) $_SESSION["MsSqlSupplierId"];

// I assume all those ID fields are integers, hopefully unsigned, correct?
$sql = "INSERT INTO camplibrary (SupplierRecId, MsSqlSupplierId, CountryRecId, BrandRecId, RatingRecId, TravelRecId)
               values($supplerid, $sqlsupplierid, $country, $brands, $rating, $travel)";
$sql_rs = $databaseConnection->qu1ery($sql);

if($sql_rs && $sql_rs->insert_id > 0)
{
    $baderr = "SUCCESS - RECORD SAVED";
                    echo "<h7 style='color:'red';alignment-adjust: 'middle'>$baderr</h7>";
                      mysqli_close($databaseConnection);
}
else
{
     print_r($databaseConnection->error_list);
}

Open in new window

Thank you for responding and posting good links... they are in my library.
I was able to resolve the problem by making sure I wasn't mixing MySQL_ with mysqli_ commands. Once I changed them all to mysqli_ commands, the access denied error disappeared. Now I have a different problem which I will post shortly...sigh
This was good information. The actual solution was that I had mixed MySQL_  (historical API) commands with mysqli_ ( the newer  object-oriented version of MySQL_ historical API) commands.  Once changed them all to mysqli_ commands, access error went away...
We've all done it before, but that little "i" is hard to see when you're not using monospace font! Glad it's resolved.