|
||||||||||||||||||||||||||||||||||||||||||||
เรียนรู้เกี่ยวกับการเขียนโปรแกรมด้วย PHP
ร่วมกับ MySQL Before getting any further into database coding, it will be good practice to call upon the PHP command of include. The first part of server connection, database selection, and table selection is a repeated snippit, so to save time and effort, we'll put that part into a seperate file and just call it into play when we need it. db_connect.inc // set your infomation. // connect to the mysql database server. // select the specific database name we want to access. ?>
The INSERT command is used to enter new information into a database. A common way to gather information is using a form. form_page.php enter_it.php // connect to server, database, table. // add the new information into the database echo "<br />";
$query="INSERT INTO address_book (first_name,last_name,phone_number)
VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[phone_number]')"; $query="INSERT INTO address_book (first_name,last_name,phone_number)
VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[phone_number]')"; $query="INSERT INTO address_book (first_name,last_name,phone_number)
VALUES ('$_POST[first_name]','$_POST[last_name]','$_POST[phone_number]')"; if(!mysql_db_query($dbname,$query,$link_id)) die(sql_error()); mysql_db_query is a command used to perform the actual insert command.
it uses the database name, query command, and server link to make it
all happen. On the INSERT tutorial page, you've already seen a QUERY in action. The query asked the database to insert the new information into the specified database table. The SELECT property can be used in a mysql_query command to choose specific information in the database table. $sql = mysql_query("SELECT * FROM table_name"); To look at each row in the specified area, the next step is to use a loop and the mysql_fetch_row command with the SELECT information. while ($row = mysql_fetch_row($sql)) { The mysql_fetch_row command will take the supplied information and read the data from the next available row of the database. The ECHO command being used to display the data during each row loop is a simple one for example purposes. The columns of a database are numbered starting at zero. The variable $row is assigned the current row contents during each loop.
The WHERE property of a query makes it possible to single out specific rows. $sql = mysql_query("SELECT * FROM table_name WHERE some equation");
while ($row = mysql_fetch_row($sql)) { The equation used for the WHERE property
can be a large number of possibilities. You can use variables, additional
equations with &&, alternative
equations with ||, and so on. = equals
while ($row = mysql_fetch_row($sql)) { $sql = mysql_query("SELECT * FROM address_book WHERE first_name='David' && last_name='Stanley'"); while ($row = mysql_fetch_row($sql)) { Doing a search through data is easy enough when the equations produce a definite yes or no situation. There may be times though you will want to perform a more general search. The LIKE and NOT LIKE have two search helper symobls. The underscore _ character that looks for one character and the percentage % character that looks for zero or more characters. $sql = mysql_query("SELECT * FROM table_name WHERE columnname LIKE value%"); while ($row = mysql_fetch_row($sql)) { $sql = mysql_query("SELECT * FROM address_book WHERE last_name LIKE 'Stan%'"); while ($row = mysql_fetch_row($sql)) { Queries using the LIKE or NOT LIKE parameters may be a bit slower than a normal query search considering they are a broader value and do not take advantage of any indexing. The underscore and percentage characters (also known as wildcard characters) can be used in front, at the end, or both ends of a value. If you want to have an underscore or percentage character actually be part of the search value, put an escape slash \ in front of the character. The underscore wildcard can be used a number of times to find a specific
number of characters. Example, this would be used in an equation to return
a value of 'Stan' plus 3 characters (since there are 3 underscores)... $sql = mysql_query("SELECT * FROM address_book WHERE last_name LIKE 'Stan___'"); while ($row = mysql_fetch_row($sql)) { Many people refer to a database as a "relational" database. More than one table is in relation to another table. Two smaller pieces of information that make a whole when combined. There are going to be times when you will want to take a bit of information from two (or more) tables with a certain criteria. Using an address book for example, you may have one table of friends and another table of how much money they owe you. You can set the criteria to find a specific criteria that appears in both tables and produce the results. You are "joining" the information from both tables to create a full result.
A popular join is called the INNER JOIN. $sql = mysql_query("SELECT * FROM phone, owes WHERE phone.first_name=owes.first_name"); while ($row = mysql_fetch_row($sql)) { In total, there are 4 columns of information being looked at. The above example is printing out all of the columns just to show which information is being passed into the results. You may choose exactly which data to use and display as required for a real application. The following results would be created... The important part of an INNER join is that if no matches are found, no results are created. Another form of join is called the LEFT JOIN. It behaves in the same way as the inner join, but it will produce extra results if there is a match in one table but not the other. $sql = mysql_query("SELECT * FROM phone LEFT JOIN owes ON phone.first_name=owes.first_name"); During an inner join, only records matching in both tables would be placed into the results. During a left join, all of the records of the first table will be placed into the results. If the records match in the second table, they will be added into the results otherwise a NULL result will be added. The results of a LEFT JOIN using the above database information would
produce these results... Again, the example is printing out all 4 columns of pertinent information.
In a real application, you would probably just print out the essential
columns. |
||||||||||||||||||||||||||||||||||||||||||||
Home เวบไซค์นี้ใช้เก็บรวบรวมข้อมูลส่วนตัวเท่านั้น มิได้มีจุดประสงค์ทางการค้าใดๆทั้งสิ้น ข้อมูลทั้งหมดในเวบนี้เป็นการเก็บรวบรวมไว้ ไม่ได้รับประกันว่าจะถูกต้องตามหลักการด้านวิศวกรรมรถยนต์ หากมีความผิดพลาดในเรื่องของข้อมูลในเวบนี้ ไม่ใช่ความรับผิดชอบของผู้จัดทำ [email protected] |