Step 5 - PHP MySQL database example
________________________________________
PHP MySQL tutorial
Here you can find a complete code which demonstrates all the 4 basic SQL statements and how to use PHP to modify data in a MySQL database.
Code:
1. ";
6. $sql = "INSERT INTO users (name,city,web,age) VALUES ('Tom','Vegas','www.tom.com',5)";
7. $result = mysql_query($sql);
8.
9. if ($result){
10. echo "Data was inserted!
";
11. } else {
12. echo "An error occured during insert!
";
13. }
14.
15. $result = mysql_query("SELECT * FROM users");
16.
17. while($row = mysql_fetch_assoc($result)){
18. echo "ID: ".$row['id'].", Name:".$row['name']
19. .", City:".$row['city'].", Age:".$row['age']."
";
20. }
21.
22. echo "
Step 2. Update data
";
23. $sql = "UPDATE users SET age=45 WHERE name='Tom'";
24. $result = mysql_query($sql);
25.
26. if ($result){
27. echo "Data was updated!
";
28. } else {
29. echo "An error occured during update!
";
30. }
31.
32. $result = mysql_query("SELECT * FROM users");
33.
34. while($row = mysql_fetch_assoc($result)){
35. echo "ID: ".$row['id'].", Name:".$row['name']
36. .", City:".$row['city'].", Age:".$row['age']."
";
37. }
38.
39. echo "
Step 3. Remove data
";
40. $sql = "DELETE FROM users WHERE name='Tom'";
41. $result = mysql_query($sql);
42.
43. if ($result){
44. echo "Data was removed!
";
45. } else {
46. echo "An error occured during remove!
";
47. }
48.
49. $result = mysql_query("SELECT * FROM users");
50.
51. while($row = mysql_fetch_assoc($result)){
52. echo "ID: ".$row['id'].", Name:".$row['name']
53. .", City:".$row['city'].", Age:".$row['age']."
";
54. }
55.
56. ?>
________________________________________
PHP MySQL tutorial
Here you can find a complete code which demonstrates all the 4 basic SQL statements and how to use PHP to modify data in a MySQL database.
Code:
1. ";
6. $sql = "INSERT INTO users (name,city,web,age) VALUES ('Tom','Vegas','www.tom.com',5)";
7. $result = mysql_query($sql);
8.
9. if ($result){
10. echo "Data was inserted!
";
11. } else {
12. echo "An error occured during insert!
";
13. }
14.
15. $result = mysql_query("SELECT * FROM users");
16.
17. while($row = mysql_fetch_assoc($result)){
18. echo "ID: ".$row['id'].", Name:".$row['name']
19. .", City:".$row['city'].", Age:".$row['age']."
";
20. }
21.
22. echo "
Step 2. Update data
";
23. $sql = "UPDATE users SET age=45 WHERE name='Tom'";
24. $result = mysql_query($sql);
25.
26. if ($result){
27. echo "Data was updated!
";
28. } else {
29. echo "An error occured during update!
";
30. }
31.
32. $result = mysql_query("SELECT * FROM users");
33.
34. while($row = mysql_fetch_assoc($result)){
35. echo "ID: ".$row['id'].", Name:".$row['name']
36. .", City:".$row['city'].", Age:".$row['age']."
";
37. }
38.
39. echo "
Step 3. Remove data
";
40. $sql = "DELETE FROM users WHERE name='Tom'";
41. $result = mysql_query($sql);
42.
43. if ($result){
44. echo "Data was removed!
";
45. } else {
46. echo "An error occured during remove!
";
47. }
48.
49. $result = mysql_query("SELECT * FROM users");
50.
51. while($row = mysql_fetch_assoc($result)){
52. echo "ID: ".$row['id'].", Name:".$row['name']
53. .", City:".$row['city'].", Age:".$row['age']."
";
54. }
55.
56. ?>
Explore more on PHP form creation and handling :
إرسال تعليق