Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJenny Swift
21,999 Pointsworking with a database on a real (not local) website
Hi, am familiar with working with a database on my computer using Mamp, but I am unsure how to work with a database on a real website. My understanding is Mamp is only local and can't be used for a real site? I am using webhostingforstudents.com as my host, and perhaps it has mysql because it can run wordpress, but I'm not sure how to work with it-how to query the database and view it like I can with phpmyadmin from Mamp. Can anyone please advise me in the right direction or point me to step by step instructions on how to do this? Does Treehouse cover this?
2 Answers
Dinesh Devkota
17,928 PointsCouldn't really tell why your MySQl server is denying the mysqli_connect. I would recommend using PDO objects to use for connection to DB as it have feature to prohibit SQL injection. I would suggest to go through this course. Php with MysQL course in treehouse.
Jenny Swift
21,999 PointsOkay, thanks Dinesh!
Edward Burton
8,296 PointsHi,
Are you trying to connect to the database from your local environment as you develop or is the php file on the server? The reason I ask is that the error message you are getting almost looks like an error you would get if trying to connect to your database from a remote location. This won't work unless you specifically tell mysql you want to allow external connections.
Hope this helps.
Jenny Swift
21,999 PointsHi Edward, thanks for the reply! Well I created a php file on my computer and uploaded it with filezilla to my public_html folder that I got when I signed up with web hosting for students. I then open my website in my browser and that's when I get the error message saying I'm not allowed to connect to the MySQL server. If I go into cpanel I can do things like creating a new database or a table with no problem, but it is specifically connecting to the MySQL database from the php file that I want to know how to do. I hope that answered your question. I'm not sure what it means to try and connect to the database from a remote location.
Jenny Swift
21,999 PointsJenny Swift
21,999 PointsUpdate: I have found phpmyadmin in cpanel but I don't know how to access my database from my php file. I am using
$con=mysqli_connect(host, username, password); // If connection fails if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
and I get the following error message:
Failed to connect to MySQL: Host 'xxx.xxx.xxx.xxx' is not allowed to connect to this MySQL server.