?? beginners_guide_to_mysql_and_php.htm
字號:
<html>
<head>
<title>Beginnerz Guide To MySQL and PHP</title>
</head>
<body>
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" width="700">
<tr>
<td>
<p align="center">
<FONT FACE = "verdana, TIMES, SANS-SERIF" SIZE = "2">
<FONT SIZE="+1">Beginner's Guide To MySQL and PHP</FONT><BR>
<FONT SIZE="1">by <A HREF="mailto:wj@314interactive.com">W.J. Gilmore<br>
</A></FONT>
</font><font size="1" face="verdana, TIMES, SANS-SERIF"> </font></td>
</tr>
<tr>
<td>
<FONT FACE = "verdana, TIMES, SANS-SERIF" SIZE = "2">
<P>
We've assembled a series of thoughts and examples based upon my own experience using Php 3.0 and MySql. Our goal is that it will aid any beginners in jumping over the common obstacles that hinder progress at the earliest stages of Php/MySql implementation.
<P">Connecting to the database.</P>
Connecting to the database involves the calling of MySql commands via Php, which
are embedded in an "HTML" page. However, instead of having the *.html
extension (ex. Mywebpage.html), the php pages would end in the extension *.php3
for Php3.0, or in the extension *.phtml, if you are using Php/FI 2.0. (Note: For
the remaining part of this document, the standards for Php 3.0 will be used.)
<P>
Before we begin to call commands, however, it is assumed that MySQL and Php have been installed and configured on the server. (If this has not been done, it would be wise to first install the latest versions, or ask your provider if MySQL and Php are available on the server.)
<P>
An simple example of the connection process would be:
<P>
<FONT COLOR = "#006699">
<?<BR>
# declare some variables<BR>
$site = io.incluso.com; <BR>
$username = mannymoejack; <BR>
$password = doodad; <BR>
$dbName = incluso; <BR>
# end variable declaration<BR>
mysql_connect($site, $username, $password); <BR>
?>
</FONT>
<BR>
That's it! You are now connected to the MySql database. Remember that the MySQL database password is usually not the same as the account password. It is also possible that your MySQL database doesn't even have a password. It is strongly suggested that password protection is placed on the database. Without it, it could be fairly (very) easy for someone to enter it by simply executing basic Php commands.
<P>Now What?</P>
You will probably want to execute commands on the database once the connection
has been made. Let's look at a couple of examples:
<BR><BR>
Let's use this as the example MySQL table:
<P>
<FONT COLOR = "#006699">
Create table rocketships (
model VARCHAR (25),
year DATE,
seating INT,
color VARCHAR (10)
); </FONT>
<P Class = "subhead">Insertion of Data</P>
Before inserting data into the rocketships table, the table must first be
created within the MySQL database. If you are unfamiliar with the necessary
steps, consult Section 7.6 of the MySQL documentation.
<BR><BR>
Let's add a new rocket to the table: (commands would be inserted where one sees
the above "#add commands here")
<BR><BR>
<FONT COLOR = "#006699">MYSQL($dbName,"INSERT INTO rocketships VALUES('X-Wing', '1998-09-14', '6', 'blue')");</FONT><BR><BR>
Important: When inserting data into the table, the number of variables inserted
into the table must match exactly with the actual number of columns within the
table. If it is not exact, the data will not be inserted.
<BR><BR>
<P>Data Mining</P>
Assuming the insertion went okay, let's find that data within the database,
inserting the following commands within the script:
<BR><BR>
<FONT COLOR = "#006699">
<?<BR>
$result = mysql($dbName, "SELECT * from rocketships where (color = 'blue' && seating = '6')"); <BR>
# array holding all rows found within the table
<BR><BR>
$num = mysql_numrows($result); <BR>
# the actual number of rows held in the array $result. <BR>
<BR><BR>
$model = mysql_result($result,$i,"model");<BR>
$color = mysql_result($result,$i,"color");<BR>
$seating = mysql_result($result,$i,"seating");<BR>
<BR><BR>
print "The model is called: $model.<BR><BR>";<BR>
print "The color is: $color. <BR><BR>";<BR>
print "The rocket seats: $seating. <BR><BR>";<BR>
<BR><BR>
?> </FONT><BR><BR>
Output: <BR><BR>
<FONT COLOR = "#006699">The model is called X-Wing. <BR><BR>
The color is blue. <BR><BR>
The rocket seats 6. <BR><BR></FONT>
Note: The above script assumes that there is only one rocket within the table
colored blue and seating 6. If there is more than one rocket having these
characteristics, the script will display the first rocket found within the
table.
<BR><BR>
Also, note within the select statement that color and seating are NOT variables.
Rather, these are the actual names of the columns. Therefore, do not place $
before the names!
<BR><BR>
Assuming there are a number of rockets having these characteristics, let's find
them all and print them all out.
<BR><BR>
<FONT COLOR = "#006699">
<?<BR>
$result = mysql($dbName, "SELECT * from rocketships where (color = 'blue' && seating = '6')");<BR>
# array holding all rows found within the table<BR><BR>
$num = mysql_numrows($result); <BR>
# the actual number of rows held in the array $result. <BR>
<BR><BR>
$ i = 0; <BR>
while ($i < $num) : <BR>
<BR>
$model = mysql_result($result,$i,"model");<BR>
$color = mysql_result($result,$i,"color");<BR>
$seating = mysql_result($result,$i,"seating");<BR>
<BR><BR>
print "Rocket number $num found:<BR><BR>";<BR>
print "The model is called: $model. <BR><BR>";<BR>
print "The color is: $color. <BR><BR>"<BR>
print "The rocket seats: $seating. <BR><BR>";<BR>
<BR><BR>
$i++; <BR>
<BR>
endwhile; <BR>
?> <BR>
</FONT>
<BR>
Up until now, we have been looking at somewhat static uses of the MySQL
database. But what if you want to allow the user to input data? This is the
subject of the next section, Forms and MySQL.
<P Class = "subhead">Variables: from a form to MySQL</P>
Many times, we do not want to statically insert data into a MySql table. This
could be easier done using telnet. We might want to use an HTML form to allow a
user to insert for example, their name and email address into a MySql table.
<BR><BR>
Let's use the following table: <BR><BR>
<FONT COLOR = "#006699">
Create table addresses (
<BR><BR>
name VARCHAR (25), <BR>
email VARCHAR (25), <BR>
date_inserted DATETIME<BR><BR>
); <BR></FONT>
<BR>
<FONT COLOR = "#006699">
<FORM ACTION="thanks.php3" METHOD="POST"<<BR><BR>
<INPUT TYPE="text" NAME="name" SIZE="25" MAXLENGTH="30" VALUE=""><BR><BR>
<INPUT TYPE="text" NAME="email" SIZE="25" MAXLENGTH="30" VALUE=""><BR><BR>
<INPUT TYPE="submit" VALUE="default value"><BR><BR>
</FORM><BR><BR></FONT>
<BR><BR>
A user would fill out the above form, entering their name and email address. The
name would be placed within the variable $name, and the email would be placed
within the variable $email. Upon clicking the submit button, the information
would be passed to the Php3 script called thanks.php3.
<BR><BR>
<FONT COLOR = "#006699">
# thanks.php3<BR><BR>
<?<BR><BR>
# declare some variables<BR><BR>
$site = io.incluso.com; <BR>
$username = mannymoejack; <BR>
$password = doodad; <BR>
$dbName = incluso;
<BR>
mysql_connect($site, $username, $password); <BR>
<BR>
MYSQL($dbName,"INSERT INTO userinfo VALUES('$name', '$address', NULL)");
<BR><BR>
print "Your data has been added to the database. Please click <A HREF = \"http://www.314interactive.com/io/\">here</A> to return to ionline."; <BR><BR>
mysql_close();<BR>
# this closes the database connection<BR>
<BR>
?></FONT> <BR>
<P>Important Notes:</P>
<ul>
<LI> Why was the word NULL used, instead of a variable? <BR>
Because the DATETIME datatype , when used in conjunction with NULL, automatically inserts the current date and time into the database.
<LI> Notice the backslashes before the " marks. The " marks are
special characters for Php, and must be noted by the backslash, or an error
will occur.
<P>Two Quick Tips:</P>
<OL>
<LI> Watch the extensions! <BR>
Sometimes, Internet providers have installed both versions 2.0 and 3.0 of Php on the server, for reasons of convenience to users who have implemented Php 2.0 in the past, and have not yet upgraded to the recent version. Therefore, if you are using commands which were found in the 3.0 manual, make sure the extensions of the pages end in *.php3. Those ending in *.phtml will only use the 2.0 parser, of which many 3.0 commands are not found! <BR><BR>
<LI> The addslashes() and stripslashes() function. <BR>
Certain characters are considered special within Php, and special consideration must be taken when attempting to pass them as variables within a Php script. For example, if I attempt to insert the following data into a MySQL database: <BR>
Ciao m'a'r'e'<BR>
It will seem as if the data has been inserted, as there will be no error. However, that data has not been passed!</OL>
There are two choices to remedy the situation:<OL>
<LI> Define the MAGIC_QUOTES variable within the configuration file.
<LI>Use the addslashes() function.</OL>
Ex. <BR>
Let's assume the following data has been passed via a form: <BR>
<FONT COLOR = "#006699">$property = Alessia's house<BR>
$where = Italy<BR></FONT>
<BR><BR>
Before the Insert statement is called, $property must undergo the addslashes()
function. <BR>
<FONT COLOR = "#006699">$property = addslashes($property);</FONT> <BR>
<BR><BR>
effectively rendering $property = Alessia\'s house.
<BR><BR>
Furthermore, when later calling that data from the database, the
stripslashes() function must be called. <BR>
<FONT COLOR = "#006699">$property = stripslashes($property);</FONT> <BR>
effectively rendering $property to again be Alessia's house. <BR>
<BR>
That's it for now. Next issue, I will continue to look at more complicated
aspects of the insert and select commands. Hope to see you there. <BR><BR>
<P Class = "subhead">F.Y.I:</P>
The MySql guide can be found at: <A HREF="http://www.mysql.com">http://www.mysql.com</A><BR><BR>
The Php 3.0 guide can be found at: <A HREF="http://www.php.net">http://www.php.net</A></font>
</ul>
<p> </td>
</tr>
<tr>
<td>
<hr noshade color="#000000">
</td>
</tr>
<tr>
<td>
<p align="center"><font face="verdana, TIMES, SANS-SERIF" size="1"><b>#thelibrarY</b></font></td>
</tr>
</table>
</center>
</div>
<FONT FACE = "verdana, TIMES, SANS-SERIF" SIZE = "2">
<p><BR>
</p>
</body>
</html>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -