0

I am a beginner with PHP and with mySQL. I am trying to do something very simple. I have read through many solutions on Stack Overflow and I have tried many different things but nothing has worked. I am simply trying to connect to MySQL with PHP. I have a MySQL Active instance running. I am using a Mac. I can successfully connect to it using the following commands from the terminal:

mysql -u root -p

This then prompts me to enter my password:

Enter password: 

I then type in my password. It is a throwaway password. It is "Snow1234". It then works successfully. It shows

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
....
mysql>

I can then enter commands in my terminal which all work correctly.

I am trying to do the same thing with php (keep in mind that I am a beginner and I have never used it before).

I have a file called form.php.

form.php:

<?php
mysql_connect('localhost','root','Snow1234');
?>

I save the file. Then, I upload the file to a URL that I own using Filezilla. Then, I visit the URL in my browser (Google Chrome):

http://www.myWebsite.com/form.php

However, it says on the website:

ERROR: Could not connect. Access denied for user 'root'@'localhost' 
(using password: YES)

How do I fix this?

halfer
  • 19,824
  • 17
  • 99
  • 186
Everett
  • 387
  • 1
  • 4
  • 17
  • 2
    To start, `mysql_*` functionality has been deprecated as of PHP 5.5 and removed as of 7 - use `mysqli` or `pdo`, secondly, make sure that your mysql database is up and running (access it through your localhost dashboard if using something like wampp or xampp) – Can O' Spam Jul 04 '18 at 15:43
  • 2
    Do not use the `mysql_***` family of functions as they are depreciated and have been removed as of 7.x, in favor of using [`mysqli_***`](http://php.net/manual/en/book.mysqli.php) or [`PDO`](http://php.net/manual/en/book.pdo.php) Assuming you're not attempting to connect to an external database. try replacing `localhost` with `127.0.0.1`, typically the user account will have an entry for localhost and 127.0.0.1, but in some cases localhost is not defined. – Will B. Jul 04 '18 at 15:44
  • For troubleshooting, from the terminal issue the following query. `SELECT \`user\`, \`host\` FROM \`mysql\`.\`user\`;` This will show you the username and host you would use in your `mysqli_connect()` function in php You should see something like `root, ::1`, `root, 127.0.01`, `root, localhost`. – Will B. Jul 04 '18 at 15:55
  • Use PDO as an absolute minimum. Even better, use an ORM like [RedBeanPHP](https://redbeanphp.com/), [Doctrine](http://www.doctrine-project.org/), [Propel](http://propelorm.org/) or [Eloquent](https://laravel.com/docs/master/eloquent). – tadman Jul 04 '18 at 16:00
  • 1
    If you are attempting to connect to a MySQL server from your hosting provider's web server. Please contact your hosting provider to configure the MySQL server for you to access. You should have an admin control panel on your hosting provider account in order to allow you to manage the MySQL server. If your MySQL server is installed on your MAC, it is not needed or recommended to use it with your hosting provider's web server. If you tell us who your hosting provider is (GoDaddy, Dreamhost, Host Gator, etc), we can link you to documentation on how to get started. – Will B. Jul 04 '18 at 16:26
  • Based on everyone's feedback I plan to use cPanel and phpMyAdmin for creating the database. Then, my form.php file (which is running on my server) will use that database. – Everett Jul 04 '18 at 16:39
  • Possible duplicate of [Mysql cannot connect - Access denied (using password yes)](https://stackoverflow.com/questions/12633020/mysql-cannot-connect-access-denied-using-password-yes) – Sumithran Jul 04 '18 at 17:52

2 Answers2

0

I see you have uploaded form.php to a server. You have to check The password and username to the database hosted at your server.

ERROR: Could not connect. Access denied for user 'root'@'localhost' 
(using password: YES)

This error is simply wrong password or username to connect to the database. You could connect to the database on your terminal, probably because you are connecting to your local database(your computer). But your form.php is now on the server attempting to connect to the database there (on the server).

You will need to access the CPANEL of your server to help you create the database etc.


Not sure if there is a need for you to have this on the server(ie accessible from anywhere on the internet). Otherwise, consider installingXampp to your computer. Then,

  1. Have the form.php stored in a folder {myproject} C://XAMPP/htdocs (depending on where you install it).
  2. Create a database through localhost/phpmyadmin
  3. Connect to the DB at your PHP side, the same way as you do now. But note, by default the username is root and password is empty.
  4. You can now access your form via http://localhost/myproject/form.php

Final Note: this is only accessible on your computer.

Mark Seah
  • 532
  • 5
  • 18
  • There is no database on my server. This is my first time ever working with a server. I am a beginner with very minimal knowledge. Are you saying I have to create a database on my server? Do I do this using Filezilla? – Everett Jul 04 '18 at 15:48
  • @Everett - if everything is local (as I suspect it is), no, you dont need a dedicated server, just your local apache setup, you don't need FTP to set it up (FTP is for file transfer, not config on a server such as DB creation), `localhost` *should* work to connect, however, username and password could be wrong for connection strings – Can O' Spam Jul 04 '18 at 15:50
  • Yes. You can't do it using Filezilla, the server should have some admin page (Cpanel) for you to do just that. You will need to have access to that page. – Mark Seah Jul 04 '18 at 15:50
  • If its all local as @samSwift as mentioned. You don't need a server also. You can try out XAMPP for a start to have your form.php stored there. – Mark Seah Jul 04 '18 at 15:51
  • I will try this out. – Everett Jul 04 '18 at 16:00
  • I think using XAMPP would be conflating the issue, since the user has a PHP web server and a means to connect to MySQL database, but unsure where exactly it is in relation to the terminal example provided. Though it is recommended to have a developer environment to be able to easily make changes before making them live. – Will B. Jul 04 '18 at 16:00
  • Seems to me that the user's PHP web server and MySQL is located at two different places, hence the attempt to connect the PHP to MySQL @localhost gave the access denied case. Therefore, for a start XAMPP can help set up such environment quick. Though, I recognize that there are other ways and tools to set such developer environment. – Mark Seah Jul 04 '18 at 16:05
  • I agree @MarkSeah, however the OP demonstrates they have a remote PHP web server they are uploading files to via FTP, and somewhere there is a MySQL DBS the OP is connecting to. We don't know where the MySQL DBS is though. So the OP is confused with what `local` and `remote` environments are. Effectively the PHP web server, depending on hosting provider, should have a means to create a database on it, especially since it is already showing an access denied error. So XAMPP would not resolve the issue of uploading to the remote PHP web server and connecting to a MySQL DBS. – Will B. Jul 04 '18 at 16:12
  • @fyrye. I see what you mean. Yes, I agree as well XAMPP does not solve the issue of uploading to the remote PHP web server and connecting to a MySQL DBS. Guess, I am trying to present an either-or for the OP. Either he/she sets up the dev environment or figure out the DBS on his/her server. – Mark Seah Jul 04 '18 at 16:19
0

Please refer this link will help you.

1.Open a Connection to MySQL

2.MySQLi Procedural

3.PDO Connection

[https://www.w3schools.com/php/php_mysql_connect.asp]