Thank You Web Monkey
http://www.webmonkey.com
I Have made a copy of these how to dues because of the awesome
direction it gives for php learning
|
PHP
By Web Monkey
Page 6 Pull It Back Out
--------------------------------------------------------------------------------
OK, now we've got our data in the database. Let's do something
with it. Copy and paste the following into a text file and
save it in your Web server document tree with a .php3 extension.
<html>
<body>
<?php
$db
= mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$result
= mysql_query("SELECT * FROM employees",$db);
printf("First
Name: %s<br>\n", mysql_result($result,0,"first"));
printf("Last
Name: %s<br>\n", mysql_result($result,0,"last"));
printf("Address:
%s<br>\n", mysql_result($result,0,"address"));
printf("Position:
%s<br>\n", mysql_result($result,0,"position"));
?>
</body>
</html>
Let's explain what happens here. The mysql_connect()
function opens a link to a MySQL server on the specified host
(in this case it's localhost) along with a username (root).
If you needed to specify a password, you'd add it here as
well. The result of the connection is stored in the variable
$db.
mysql_select_db()
then tells PHP that any queries we make are against the mydb
database. We could create multiple connections to databases
on different servers. But for now, let's leave it to this.
Next, mysql_query() does all the hard work. Using the database
connection identifier, it sends a line of SQL to the MySQL
server to be processed. The results that are returned are
stored in the variable $result.
Finally, mysql_result() is used to display the values of
fields from our query. Using $result, we go to the first row,
which is numbered 0, and display the value of the specified
fields.
The syntax of the printf function may seem a little odd if
you haven't used Perl or C before. In each of the lines above,
%s indicates that the variable in the second half of the expression
(e.g., mysql_result($result,0,"position")) should
be treated as a string and printed. For more on printf, see
the PHP documentation.
So there we have it. We successfully complied, installed,
and configured MySQL and PHP, and we've executed a simple
script to retrieve some information. In Lesson 2, we'll do
some clever stuff to display multiple records and even send
data to and from the database.
Come on back, now.
|
Thank You Web Monkey
http://www.webmonkey.com
I Have made a copy of these how to dues because of the awesome
direction it gives for php learning
|