How to use PHP to access MySQL?


#1

Forgive me if this seems like a very simple question, but I am having a lot of trouble figuring out how to access a MySQL database I’ve created on DreamHost.

As the membership coordinator for a local nonprofit, I’m making a page for our web site that will allow people to sign up online. I’ve created a form to input the data, and tested it using DreamHost’s form-to-email application (it works fine). But what I really want to do is put that data in a MySQL database.

This seems simple enough, and there’s tons of info on using PHP to do this. But nobody seems to say where the PHP script goes on my web server, or how it can access the MySQL database – which requires a login and password. I obviously don’t want people to be able to see this info if it’s embedded in the PHP script (which, it seems to me, it must be). How is this done? Do I create some sort of password-accessible subdirectory? If so, how do I hide the password that allows access to that? I don’t get it.

Sorry if this is obvious to everyone else. I’m really new at this! Thanks in advance for any help.

-Craig


#2

A password done in PHP is only available to those who have FTP access. It can not be seen by the general public, though i still keep my connection script in a folder that’s not publicly accessible (either via .htaccess or having it in the root folder)

The php script is placed in the same folder as the rest of your files

To connect to the database is simple, use the following php code

[quote]$con = mysql_connect(‘dbhost’, “dbusername”, “dbpassword”) or die(‘Could not connect to database!’);
$db = mysql_select_db(“dbname”, $con);[/quote]


#3

There is a useful document on the DreamHost Wiki that might help you: http://wiki.dreamhost.com/MySQL_and_PHP

si-blog
Max discount on any plan with promocode SCJESSEYTOTAL