Học php, mysql và javascript - p 27

$query = "INSERT INTO cats VALUES(NULL, 'Cougar', 'Growler', 2)"; $query = "INSERT INTO cats VALUES(NULL, 'Cheetah', 'Charly', 3)"; By the way, notice the NULL value passed as the first parameter? This is done because the id column is of the AUTO_INCREMENT type and MySQL will decide what value to assign according to the next available number in sequence, so we simply pass a NULL value, which will be ignored. Of course, the most efficient way to populate MySQL with data is to create an array and insert the data with a single query. Retrieving Data Now that some data has been entered into. | query INSERT INTO cats VALUES NULL Cougar Growler 2 query INSERT INTO cats VALUEs nULL Cheetah Charly 3 By the way notice the NULL value passed as the first parameter This is done because the id column is of the AUTO_INCREMENT type and MySQL will decide what value to assign according to the next available number in sequence so we simply pass a NULL value which will be ignored. Of course the most efficient way to populate MySQL with data is to create an array and insert the data with a single query. Retrieving Data Now that some data has been entered into the cats table Example 10-13 shows how you can check that it was correctly inserted. Example 10-13. Retrieving rows from the cats table php require_once db_server mysql_connect db_hostname db_username db_password if db_server die Unable to connect to MySQL . mysql_error mysql_select_db db_database or die Unable to select database . mysql_error query SELECT FROM cats result mysql_query query if result die Database access failed . mysql_error rows mysql_num_rows result echo table tr th Id th th Family th th Name th th Age th tr for j 0 j rows j row mysql_fetch_row result echo tr for k 0 k 4 k echo td row k td echo tr echo table This code simply issues the MySQL query SELECT FROM cats and then displays all the rows returned. Its output is as follows Id Family Name Age 1 Lion Leo 4 2 Cougar Growler 2 3 Cheetah Charly 3 Here you can see that the id column has correctly auto-incremented. Practical MySQL 241 Updating Data Changing data that you have already inserted is also quite simple. Did you notice the spelling of Charly for the Cheetah s name Let s correct that to Charlie as in Example 10-14. Example 10-14. Renaming Charly the Cheetah to Charlie php require_once db_server mysql_connect db_hostname db_username db_password if db_server die Unable to connect to MySQL . mysql_error mysql_select_db db_database or die Unable to select database . mysql_error query UPDATE cats SET name Charlie WHERE name

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.