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
OverthereAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

F PCommented:
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

F PCommented:
http://php.net/manual/en/function.include.php


Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing. The include construct will emit a warning if it cannot find a file; this is different behavior from require, which will emit a fatal error.

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

For more information on how PHP handles including files and the include path, see the documentation for include_path.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
F PCommented:
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.
Active Protection takes the fight to cryptojacking

While there were several headline-grabbing ransomware attacks during in 2017, another big threat started appearing at the same time that didn’t get the same coverage – illicit cryptomining.

OverthereAuthor Commented:
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...
F PCommented:
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.
F PCommented:
<?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

OverthereAuthor Commented:
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
OverthereAuthor Commented:
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...
F PCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.