Monday, March 28, 2011

Accessing MySQL Databases with PHP

Once you know how to do these three things (and if you have you would be advised to recap before going further), we can now put it all together create an online database application.
To continue, you will need to ensure that the hms database we discussed on page 205 exists the web server, and that there’s a table within it called customers. The customers table 7 fields:
id
firstname
surname
title
address
phone
email
All of these should be text fields (tinytext should suffice) except id, which should be an Further, id should be set as an auto-increment field. If you’ve forgotten how to do refer back to page 210 for coverage of phpMyAdmin. the database and table created, your phpMyAdmin screen should look like this when you view the structure of the Customers table:

You’ll also need some data in the table. Hopefully, your data will still be there from when we covered phpMyAdmin. If not, browse to the Customers table, click on Insert, and create a couple of records. Now, when you click on Browse, you should see the data in the table like this:

Counting Rows

Now we’re ready to start writing some real code. Close your Web browser, open PSPad, and
create a file in your public_html folder called custlist.php which contains the following:
<?php
$db_host = "localhost";
$db_database = "thewebbo_hms";
$db_username = "thewebbo";
$db_password = "abc123xyz";
$dbcnx = mysql_connect($db_host,$db_username,$db_password);
mysql_select_db($db_database);


$query = "select id from customers";
$q = mysql_query($query);
$total = mysql_num_rows($q);


echo "Total number of customers in the database is " . $total;

?>

Open your web browser and surf to www.yoursite.com/custlist.php and you should see the following:




0 comments:

Post a Comment

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms