« Home « Kết quả tìm kiếm

Website Database Basics With PHP and MySQL


Tóm tắt Xem thử

- You just type in the fields you want.
- Most word processors will "Save As…".
- We'll start with printing "Hello, world.".
- In your text editor, create a document called helloworld.php .
- In the document, enter the following code:.
- print "Hello, world.";.
- You should see "Hello, world.".
- in the browser window..
- Connect to the Internet..
- Transfer the file (using Fetch or equivalent) helloworld.php to your server.
- You could use your terminal emulator and a UNIX text editor to create the document helloworld.php on the UNIX server, but I find it easier to use my computer's text editor and then transfer the file..
- Use your browser to go to the file helloworld.php on your UNIX server.
- I.e., use Internet Explorer or Netscape Navigator to go to http://www.yourwebsite.com/helloworld.php.
- in the browser window.
- Use Fetch or your terminal emulator to make sure the file helloworld.php is on your UNIX server.
- Welcome to the MySQL monitor.
- Look in the MySQL documentation..
- Let's examine how helloworld.php worked.
- sets out the body of the HTML document, which is displayed to the user (as opposed to the header's invisible information)..
- Alternative means to the same end.
- helloworld.php is an HTML document with a embedded PHP script.
- print is the PHP function that sends text to the browser.
- There are several other ways to send "Hello, world.".
- The following file is print.php.
- print "This uses the print function.";.
- if you separate strings with a comma.";.
- printf ("This uses the printf function.");.
- printf ("The printf function is mostly used to format numbers.");.
- printf ("Remember the parentheses with printf.");.
- print.php produces the following output:.
- Printing strings and numbers is easy, but how do you print arrays? (Arrays are sets of things.) If you try.
- You must create the function to be executed — you can't use a PHP function such as print.
- <form action=submitform.php method=GET>.
- First Name: <input type=text name=first_name size=25 maxlength=25>.
- First Name: Last Name:.
- When you enter data in the fields and then click Submit , the form sends the data to submitform.php .
- Here is submitform.php.
- print (".
- print ("Thanks for submitting your name.");.
- …/submitform.php?first_name=Fred&last_name=Flintstone.
- Because we used the GET form method, the fields are passed from submitform.html to the URL header of submitform.php .
- PHP variables always start with a dollar sign, so the variables are $first̲name and $last̲name.
- first_name | last_name .
- Let's examine how submitform.php works:.
- <form action=searchform.php method=GET>.
- You will also have to create the following file searchform.php.
- if ($first_name.
- first_name.
- if ($last_name.
- last_name.
- WHERE first_name LIKE '$first_name%' AND last_name LIKE '$last_name%' ");.
- Going over searchform.php carefully, it begins with the familiar lines to open the MySQL database.
- {$last_name.
- The if function is followed by parentheses, and what's in the parentheses is the statement to be tested.
- The statement $first̲name.
- means "The variable $first_name is empty.".
- A single equals sign means "assign the value from what's on the right to the on the left.".
- (Note that the if function doesn't end with a semi-colon.) The PHP line is put in curly brackets..
- (Don't confuse it with SQL's * column name wildcard.) The first two lines together mean "If the 'first name' field is empty, search for any first name.".
- WHERE first_name LIKE '$first_name%' AND last_name LIKE '$last_name%'".
- This query selects all the columns from the specified table, then searches for records in which the "first_name".
- column matches the "$first_name".
- The first time through, the first "first_name".
- mysql̲fetch̲array($result) allows you to specify elements by the field name (e.g., "first̲name".
- WHERE first_name = '$first_name' AND last_name = '$last_name' ");.
- print "Your SELECT query was successful.";.
- print "Your SELECT query was not successful.";.
- print "Your DELETE query was successful.";.
- print "Your DELETE query was not successful.";.
- In other words, visitors have to enter something in the field.
- The following script checks that a first name was entered:.
- if (ereg first_name.
- echo "First name first_name";.
- A first name is required.");.
- ereg is followed by parentheses, and you can put three arguments in the parentheses.
- or period is a regular expression wild card meaning "any character.".
- (ereg first̲name.
- 1) means "the variable '$first_name' contains anything".
- If this expression is true, then the first name is printed, and the variable $verify is set to "OK"..
- The line if (ereg first̲name.
- 1) can be simplified to if ($first̲name.
- Go back and enter a new user name.");.
- A cookie is a set of data stored in the visitor's browser.
- The cookie name, a string, e.g., "CookieID".
- The time can be an absolute time, in the format DD-Mon-YY HH:MM:SS, e.g., "Wed, 24‑Nov .
- before the domain name, e.g., ".friendshipcenter.com"..
- Cookies are stored in the array HTTP̲COOKIE̲VARS .
- Going back to our webpage submitform.php , which inserted the visitor's name into our database, let's add code to look up the USERID number our database automatically assigns to each submitted name, and then send a cookie to the visitor's browser with the value set as the USERID number..
- To set a cookie in the visitor's browser after he or she inserts his name into your database, with the value of the cookie taken from the USERID, you could do this:.
- Save this script as cookiepage.php .
- If you save this to your UNIX server, then open the webpage after running the last version of submitform.php , you should get the value of your cookie.
- Now let's make cookiepage.php welcome me by name:.
- echo "Welcome ", $row[first_name .
- In the section PHP submits data to MySQL I pointed out that when a MySQL line is part of a PHP line, the semi-colon at the end of the MySQL line of omitted.
- You can retrieve the current date, in the same format with the PHP function.
- datatype in the checkboxes chapter..
- "any single character.".
- $selectresult = mysql_query ("SELECT * FROM dbname WHERE FIRST_NAME = 'Bob' AND LAST_NAME = 'Smith'.
- print "This is my text block.";.
- include ('textblock.php');

Xem thử không khả dụng, vui lòng xem tại trang nguồn
hoặc xem Tóm tắt