Use PHP to Connect to MySQL - php

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?

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,
Have the form.php stored in a folder {myproject} C://XAMPP/htdocs (depending on where you install it).
Create a database through localhost/phpmyadmin
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.
You can now access your form via http://localhost/myproject/form.php
Final Note: this is only accessible on your computer.

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]

Related

Warning: mysqli_connect(): (HY000/1045): Access denied for user 'root'#'localhost' (using password: NO) cant find any solution [duplicate]

so i installed the MySQL application for the first time. firstly i saw the command line client is not opening so i searched for solutions. they said i must go to the bin directory and run it manually. and after i run the cmd mysql -uroot -p and run it and enter password, it gives me the error: ERROR 1045 (28000): Access denied for user 'root'#'localhost' i tried every single solution on stackoverflow including disabling permissions, running manually which i mentioned above, starting the service from service.msc, running it with password and without.... it just doesnt want to work.
appreciate any help in advance.
GENERIC MYSQL INFO
To start with, read the mysql manual: https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
The steps will show you how to shut down the service and start it with an overriding command that doesn't require passwords, then you reset the password. From the manual:
Stop the MySQL server, then restart it with the --skip-grant-tables option. This enables anyone to connect without a password and with all privileges and disables account-management statements such as ALTER USER and SET PASSWORD. Because this is insecure, you might want to use --skip-grant-tables in conjunction with --skip-networking to prevent remote clients from connecting.
Connect to the MySQL server using the mysql client; no password is necessary because the server was started with --skip-grant-tables:
shell> mysql
In the mysql client, tell the server to reload the grant tables so that account-management statements work:
mysql> FLUSH PRIVILEGES;
Then change the 'root'#'localhost' account password. Replace the password with the password that you want to use. To change the password for a root account with a different hostname part, modify the instructions to use that hostname.
MySQL 5.7.6 and later:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
MySQL 5.7.5 and earlier:
mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
Or directly on the user table:
UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';
XAMPP SPECIFIC
Stop the MySQL service. Open a command window. Change to the XAMPP MySQL directory:
> cd \xampp\mysql\bin\
Run the service without security (note you are running mysqld, not mysql):
> mysqld.exe --skip-grant-tables
The MySQL service will be running in this window, so open another command window and switch to the XAMPP MySQL directory:
> cd \xampp\mysql\bin\
Run the MySQL client:
> mysql
Update the password:
mysql> UPDATE mysql.user SET password=PASSWORD('mynewpassword') WHERE user='root';
Exit MySQL:
mysql> \q
Use task manager to cancel the mysqld.exe that is still running. Restart the mysql service.
I got the answer myself. Seemingly, if you get this error, it means that you need to reset your password. You can learn how to do that in MySQL from this link.
And don't forget to change the 5.7 version with your currently installed version in using commands (mine was 8.0).
After that, everything was working fine for me.

Raspberry Pi Database Server

I would like to host a database on my raspberry pi to which I can access from any device. I would like to access the contents of the database using python.
What I've done so far:
I installed the necessary mysql packages, including apache 2.
I created my first database which I named test.
I wrote a simple php
script that connects and displays all the contents of my simple
database. The script is located on the raspberry pi at /var/www/html
and is executed when I enter the following from my laptop
(192.168.3.14/select.php)
Now my goal is to be able to connect to the database using python from my laptop. But I seem to have an error connecting to it, this is what I wrote to connect to it.
db = MySQLdb.connect("192.168.3.14","root","12345","test" )
Any help or direction is appreciated.
on the terminal of your raspi use the following command:
mysql -u -p -h --port
where you switch out your hostname with your ip address. since currently you can only connect via local host
at first step is check you haven't firewall rules on raspberry or in your lattop
after you can try this command on mysql
GRANT ALL PRIVILEGES ON *.* TO 'root'#'%';
and remember to apply new privileges
FLUSH PRIVILEGES;
for more detail you can see
https://dev.mysql.com/doc/refman/5.7/en/grant.html
is similar for mariadb ecc...
The reason why you are not able to connect to the database from outside of localhost is that the remote access for the root user is prohibited by default, i.e. you can only access the database with the root user from localhost. You can, however, change this by tweaking root's privileges. Please take a look here to find out how the user privileges need to be change to make this work.

Cannot connect to MySQL server on Openshift

Recently, I've started using openshift & also deployed an application using PHP & MySQL. Yesterday, out of curiosity, I've removed the password for phpMyAdmin and guess what, now I'm unable to log in to both phpMyAdmin & mysql database.
I've tried both the passwords (the default one & the empty password) and uninstalled & re-installed the PHPmyAdmin catridge & also, force restarted the app several times but nothing worked. Now, I've no idea what happened. Any help is appreciated.
Hopefully this will help.
I assume what you did was go into phpmyadmin and click on 'users' then 'edit privileges' for one of the users, select 'no password' and hit save right? If so, then I think the following steps should help.
1.) ssh into your gear (you can use the rhc ssh command)
2.) run the mysql command
3.) You should get an error like this ERROR 1045 (28000): Access denied for user 'adminslULJTS'#'127.10.126.130' (using password: YES)
4.) Now, type in the command mysql -u $OPENSHIFT_MYSQL_DB_USERNAME -h $OPENSHIFT_MYSQL_DB_HOST -P $OPENSHIFT_MYSQL_DB_PORT -p
5.) When it asks for a password, just hit enter
6.) You should now be logged into the mysql shell
Now you need to reset your password to what openshift thinks it is.
1.) create another ssh session into your gear in another terminal, leaving the old one open
2.) run the command env | grep MYSQL
3.) this will give you the following information that you will need to reset your password:
OPENSHIFT_MYSQL_DB_HOST=127.10.126.130
OPENSHIFT_MYSQL_DB_PASSWORD=Il8-rVLIKSrx
OPENSHIFT_MYSQL_DB_USERNAME=adminslULJTS
Given the above information, go back to your ssh session that had the mysql connection open, and enter the following command:
set password for 'adminslULJTS'#'127.10.126.130' = PASSWORD('Il8-rVLIKSrx');
But you will need to replace the username, host, and password with the ones you got from the above step.
You should now be able to log into phpmyadmin with your old username & password that you can either view using the env | grep MSYQL command, or view in the web console for your application at openshift.com
It also might be worth reviewing this KB article: https://www.openshift.com/kb/kb-e1085-possible-complications-when-changing-your-database-credentials

php/mySQL on XAMPP: password for phpMyAdmin and mysql_connect different?

I am running phpMyAdmin and MySQL on XAMPP for Windows.
I am accessing the MySQL database in two ways. First, via localhost/phpmyadmin, and second, via a connection.php file (with mysql_connect('localhost','user','password') which is used by other php files for a website on the server.
Both for the phpMyAdmin login and the mysql_connect I am using the root user. I know I should not be using root and that it is a security issue. But the passwords differ! I have set the password for the phpMyAdmin access via localhost/security, but I can't figure how to change the password required for the mysql_connect.
I thought it would be the same but apparently it is not. How do I change the root password through phpMyAdmin?
if you open localhost/phpmyadmin you will find a tab called "User accounts". There you can define all your users that can access the mysql database, set their rights and even limit from where they can connect.
You need to change the password directly in the database because at mysql the users and their profiles are saved in the database.
So there are several ways. At phpMyAdmin you simple go to user admin, choose root and change the password.

SQLSTATE[28000][1045] Access denied for user 'root'#'localhost'(using password:NO)

When I try to connect to my backoffice in my website I get this error :
SQLSTATE[28000][1045]Access denied for user 'root'#'localhost'(using password:NO))
Can someone help me fix this?
You must use a password. Try to set one with "mysqladmin -u root password NEWPASSWORD"
You need to supply a password when logging into your database. When you connect to sql from php, make sure you're supplying this password.
If you set a root password previously, but have forgotten it, you can set a new password. I am working on a Windows machine, and was able to reset the password on the root of MySql using the directions http://dev.mysql.com/doc/refman/5.0/en/resetting-permissions.html. I created a text file (mysql-init.txt) as follows:
UPDATE mysql.user SET Password=PASSWORD('[choose your password')
WHERE User='root';
FLUSH PRIVILEGES;
At the command line, I entered the following (either make sure you are in the mysql\bin directory or that your path environment variable includes a path to that directory), which will start the server:
mysqld --init-file=C:\\mysql-init.txt --console
This printed out a bunch of status statements. To stop the server, I typed this in another command line:
mysqladmin –u root –p[choose your password] shutdown
Of course, replace the [choose your password] with the same password you used in the mysql-init.txt file. In the first command line, you should get more status statements that indicate that the server is shutting down.
Now that you have a password for your root user, you can go back to the connection statement you are using to connect to the database and add the password and user.

Categories