+ 1
HELP: Database connection
Hello friends, I have tried to make a connection with database but nothing displayed whether connection is successful or not. I'm using XAMPP as a server(successful installed) What's missing in this code? <?php $servername="localhost"; $username="root"; $password=""; $dbname="test"; $conn = mysqli_connect($servername,$username,$password,$dbname); if($conn){ echo ("Successfully Connected"); } else{ echo ("Connection failed"); } ?>
15 Answers
+ 5
Yes bro, the file has to be in htdocs (or in its subfolder) for it to be processed by PHP.
I am not very familiar with VSC, and I used to just open the file using a browser. For example, if the filename is test.php and is saved in htdocs/practice/ folder, I access it like http://localhost/practice/test.php
Hope you get what I'm saying bro ...
+ 4
Create a folder inside htdocs and name is anything you like. We call it "site". Inside site folder create a file test.php. To view you will visit localhost/site/test.php. If it's the configuration you are having issues with please check that the database name in phpmyadmin and wp-config are the same. If you set a password then also make sure they match.
+ 4
Thank you friends, through your comments, i have been succeeded. Bless up
+ 3
I think you are getting some error. Check error and find the solution on Google.
On different version of PHP MySQL connection logic is different.
+ 3
Just to test, try to set password for root account, or create new user and set a password for it. Then try to connect again but leave <$password> empty in code. By then, there should be an error message, because empty password given when password is set.
BTW, how did you access the file? the URL you used?
+ 2
I have tried this option still there is a blank page after page loading completion.
NB: Both apache and mysql are running perfectly
+ 2
Can you save a copy of your PHP file in SoloLearn and share its link here? maybe someone else who comes here can find something we missed.
Can you take a screenshot of the XAMPP control panel, the status pane at lower bottom in particular? You can share it here by attaching the screenshot image in a feed post.
Did you try to connect to MySQL from command line? how did it work?
Not sure about this, but maybe just give it a try to restart the Apache & MySQL services or the computer, and see how it goes.
(Edited)
+ 2
Thank you again for your time Ipang,
Click this link to view the file code
https://code.sololearn.com/Wu36O103XIU5/?ref=app
+ 2
Hamimu Said bro,
Thank you for elaborating 🙏
But isn't this a web code? you saved your code in XAMPP like this?
Please also tell me the URL you used in browser address bar to access the PHP file. I happen to have met a case where it was actually simple, related to this.
+ 2
No, this file is not within the Xampp at htdocs, it is just on the other folder in desktop and running directly from text editor (VS code). Can this be one of the problem courses? I mean is it necessary to put files in xampp/htdocs for them to run?
+ 2
Thank bro for your time,bless up. I'll try again and let you know if it works or not.
+ 2
Good to hear that 👍
+ 1
No error displayed, the page is totally blank after running the code. Version 7.4.2
+ 1
$Can you check if your server ? If running and which port ....I
$Check too if you have user_creds
$list_users
$list_db ...... if All 👈 are in (place)
Then.....excute your codes one more time
+ 1
Thank you Bonface, now it is Ok. The problem was file location, it should be within htdocs folder