A database abstraction class is one that forms a layer between your program and the databases. It takes care of the different databases and their details. You only have to learn to code for one database, and it will work for all of them.
Firstly, I suggest that you rename the db_xxxxx.inc files db_xxxxx.php for security, as we are going to be putting our database details in there, and we don't really want to share them with everyone else, do we? Once you've done that, find the following lines:
/* public: connection parameters */
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
Enter you database details as shown below:
/* public: connection parameters */
var $Host = "address.to.connect.to.your.db-usually.localhost";
var $Database = "yourname";
var $User = "username";
var $Password = "***********";
You will soon be able to connect to the database - once we've done some coding!
Firstly, we'll create a table on your database. Run the following SQL query on your database:
CREATE TABLE test (
name varchar(150),
description text,
price varchar(50)
);
# Let's insert some data:
INSERT into test (name, description, price) VALUES ('Banana', 'Funny yellow
crescent shaped thing', '0.99/bunch');
INSERT into test (name, description, price) VALUES ('Pineapple', 'Lovely and
juicy', '1.69 ea');
INSERT into test (name, description, price) VALUES ('Apples', 'Many different
varieties available. Come and see our range', '0.69/pound');
INSERT into test (name, description, price) VALUES ('Oranges', 'Just arrived
straight from Israel', '1.42/Kg');
Right, now that's done we'll write some code.
Let's take a greengrocer as an example. We're going to use PHP to show what
fruit we have in stock, and print it out on the page.
<!-- Begin test.php -->
<html>
<head>
<title>Our fruit</title>
</head>
<body>
<p>This is the fruit we have available at the moment:</p>
<?php
include("db_mysql.php");
$db = New DB_SQL;
$sql = "SELECT * from test";
$db->query($sql);
while($db->next_record()) {
$name = $db->f("name");
$description = $db->f("description");
$price = $db->f("price");
echo("<p><b>Fruit:</b> ".$name."; <b>Price:</b> £".$price."<br>\n");
echo($description);
echo("</p>\n\n");
}
?>
</body>
</html>
<!-- End test.php -->
Looks complicated, doesn't it? Hopefully the following should make things clearer. Let's take one line of code at a time.
<?php
include("db_mysql.php");
$db = New DB_SQL;
These lines include the database abstraction class (in this case for MySQL),
and then create an instance of the class which will be referred to using the
variable $db.
$sql = "SELECT * from test";
$db->query($sql);
This executes our SQL query on the database.
while($db->next_record()) {
$name = $db->f("name");
$description = $db->f("description");
$price = $db->f("price");
echo("<p><b>Fruit:</b> ".$name."; <b>Price:</b> £".$price."<br>\n");
echo($description);
echo("</p>\n\n");
}
?>
This is the actual code that prints the details on the page. Let's take a section at a time:
while($db->next_record()) {
... code goes here ...
}
This tells the script that if there is another row of data returned by the
SQL query to re-run the code inside the two {} brackets. This is how it prints
out all the data.
$name = $db->f("name");
This assigns to the variable $name the value in the database column 'name'.
echo("<p><b>Fruit:</b> ".$name."; <b>Price:</b> £".$price."<br>\n");
echo($description);
echo("</p>\n\n");
This prints the list of fruits onto the page, as you have probably guessed already.
There are actually two ways to do the above, and I have shown you my favourite. It does double the lines of code needed, but I find it is easier to read. The other way is as follows:
while($db->next_record()) {
echo("<p>Fruit: ".$db->f("name")."; Price: £".$db->f("price")."<br>\n");
echo($db->f("description"));
echo("</p>\n\n");
}
?>
So what does this file show when it's been run? You can see the output here.
© 4WebHelp and Peter
Page URL: http://www.4webhelp.net/tutorials/php/phplib_db.php
Back to the "pretty" page!
© 2024, 4WebHelp Team.
name varchar(150),
description text,
price varchar(50)
);
# Let's insert some data:
INSERT into test (name, description, price) VALUES ('Banana', 'Funny yellow crescent shaped thing', '0.99/bunch');
INSERT into test (name, description, price) VALUES ('Pineapple', 'Lovely and juicy', '1.69 ea');
INSERT into test (name, description, price) VALUES ('Apples', 'Many different varieties available. Come and see our range', '0.69/pound');
INSERT into test (name, description, price) VALUES ('Oranges', 'Just arrived straight from Israel', '1.42/Kg');
Is this using MySql 4 or 5?
Add a new comment