Tuesday, December 23, 2008

(0) Comments

How To Acces a MySQL Database using PHP

welcome to infomix.blogspot.com

Notice the Connection Strings. We will be using the PHP connection string. Copy everything but the $dbh= from the PHP connection string and paste it into your PHP file. As you can see below, I have already added the basic HTML to the page. Simply paste the code between the open and close PHP tags.
How to Access MySQL Databases using PHP

To explain the string a little bit:
Mysql_connect – is a built in function to connect to a database
“localost” – tells the server to connect to the local database
“projectc_testuse” – this is my username for the database (be sure to replace it with yours)
” – this is where you need to type in the password for your database user
Or die – will return an error if we cannt connect to the database.
Mysql_select_db (”projectc_Test”); - select the database for use

Now that we have a connection to the database, we will need to build our query. We will be selecting everything from the TestTable we created in the previous tutorial. An asterisks (*) is a wildcard character that means all. So our query will look like this (note: I added comments. They will be in orange).

How to Access MySQL Databases using PHP

What did we do? We created a variable called query. Then we set the variable to be a MySQL query that will select everything from our TestTable.

Now we need to execute the query and display the results.

How to Access MySQL Databases using PHP

Ok. We are creating an array called $row. Using the mysql_fetch_array function we will execute our query and store it to our array so that we can write the data to the page. Echo will write whatever follows to the page.

I will break it down the echo string a little bit here is what we have (check the comments on each line).
How to Access MySQL Databases using PHP

That is it. Save this file with a .php extension. Upload the file to your web server and execute it in your browser. The results should look something like this.
How to Access MySQL Databases using PHP

0 Responses to "How To Acces a MySQL Database using PHP"

Post a Comment