Lệnh delete dùng để xóa các records trong MySQL, lệnh này thường dùng trong backend đẻ xóa các tin tức, sản phẩm, chuyên mục. Cú pháp của lệnh này như sau:

1

2

DELETE FROM table_name

WHERE some_columns = some_values

Thường trong ứng dụng website thì ta sẽ delete theo khóa chính bởi vì khóa chính tăng tự động nên truy vấn tìm kiếm sẽ nhanh hơn. Nếu bạn không truyền vào điều kiện where thì nó sẽ xóa hết tất cả data trong table ,vì vậy hãy kiểm tra kỹ điều kiện trước khi thực hiện lệnh này.

Nội dung chính

  • 1. Delete dữ liệu với MySQLi
  • Sử dụng MySQLi Object-oriented:
  • Sử dụng MySQLi Procedural:
  • 2. Delete dữ liệu với PDO
  • 3. Lời kết
  1. Delete dữ liệu với MySQLi

Chúng ta có hai trường hợp sử dụng đó là hướng đói tượng và hướng thủ tục truyền thống.

Sử dụng MySQLi Object-oriented:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

// Kết nối

$conn = new mysqli(‘localhost’, ‘root’, ‘vertrigo’, ‘FreetutsDemo’);

 

// Kiểm tra kết nối

if ($conn->connect_error) {

die(“Kết nối thất bại: ” . $conn->connect_error);

}

 

// Câu SQL delete

$sql = “DELETE FROM News WHERE id=1”;

 

// Thực hiện câu truy vấn

if ($conn->query($sql) === TRUE) {

echo “Xóa thành công”;

} else {

echo “Xóa thất bại: ” . $conn->error;

}

 

$conn->close();

Sử dụng MySQLi Procedural:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

// Kết nối

$conn = mysqli_connect(‘localhost’, ‘root’, ‘vertrigo’, ‘FreetutsDemo’);

 

// Kiểm tra kết nối

if (!$conn) {

die(“Kết nối thất bại: ” . mysqli_connect_error());

}

 

// Câu SQL delete

$sql = “DELETE FROM News WHERE id=1”;

 

// Thực hiện câu truy vấn

if (mysqli_query($conn, $sql)) {

echo “Xóa thành công”;

} else {

echo “Xóa thất bại: ” . mysqli_error($conn);

}

 

// ngắt kết nối

mysqli_close($conn);

  1. Delete dữ liệu với PDO

Cũng tương tự như những bài trước.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

try {

// Kết nối

$conn = new PDO(“mysql:host=localhost;dbname=FreetutsDemo”, ‘root’, ‘vertrigo’);

 

// Thiết lập exception

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

 

// Câu SQL

$sql = “DELETE FROM News WHERE id=1”;

 

// Thực thi câu truy vấn

$conn->exec($sql);

 

echo “Xóa thành công!”;

}

catch (PDOException $e) {

echo ‘Lỗi’ . “<br>” . $e->getMessage();

}

 

// Ngắt kết nối

$conn = null;

  1. Lời kết

Các ví dụ trên mình không sử dụng cơ chế prepared nên nó sẽ không bảo mật, vì vậy khi lập trình bạn nên sử dụng prepared nhé. Bài tiếp theo chúng ta sẽ tìm hiểu cách cập nhất dữ liệu bằng PHP.

——————-#####——————-

 

343 BÌNH LUẬN

  1. Pretty nice post. I just stumbled upon your weblog and wanted to say
    that I have really enjoyed browsing your blog posts.
    In any case I’ll be subscribing to your rss feed and I
    hope you write again soon!

  2. After looking into a handful of the articles on your blog, I seriously appreciate your way
    of writing a blog. I saved as a favorite it to
    my bookmark webpage list and will be checking back soon. Take a look at
    my website too and tell me what you think.

  3. Hi there! This post could not be written any better!

    Readding through this post reminds me of my good old room mate!
    He always kept talking about this. I will forward this page to him.
    Fairly certain he will have a good read. Many thanks for
    sharing!

  4. Amazing blog! Is your theme custom made or did you download it from somewhere?
    A design like yours with a few simple tweeks would really make my blog shine.
    Please let me know where you got your theme.
    With thanks

  5. An impressive share! I have just forwarded this onto a coworker who
    was doing a little research on this. And he in fact bought
    me dinner because I discovered it for him… lol.
    So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanks for spending the time to discuss this
    topic here on your website.

  6. Awesome issues here. I am very satisfied to see your article.
    Thank you so much and I am taking a look ahead to contact you.
    Will you please drop me a e-mail?

  7. Hey! This post couldn’t be written any better!
    Reading through this post reminds me of my old room mate!

    He always kept talking about this. I will forward this write-up to him.
    Pretty sure he will have a good read. Thank you for sharing!

  8. Is not it amazing whenever you discover a fantastic article? My personal web browsings seem full.. thanks. Respect the admission you furnished.. Extremely valuable perception, thanks for blogging..

  9. Im no expert, but I think you just made the best point. You definitely fully understand what youre talking about, and I can seriously get behind that. Thanks for staying so upfront and so genuine.

  10. This awesome blog is without a doubt entertaining as well as diverting. I have picked a lot of useful things out of this blog. I ad love to go back every once in a while. Thanks a lot!

  11. Nice weblog here! Also your site quite a bit up very fast! What web host are you the usage of? Can I am getting your affiliate link to your host? I desire my web site loaded up as quickly as yours lol

  12. Thanks for another great post. Where else could anybody get that type of information in such a perfect way of writing? I ave a presentation next week, and I am on the look for such info.

  13. This awesome blog is really interesting and informative. I have discovered a lot of interesting stuff out of this amazing blog. I ad love to visit it every once in a while. Thanks a lot!

  14. IaаАа’б‚Т€ТšаЂаŒаАа’б‚Т€ТžаБТžd ought to talk to you here. Which is not some thing I do! I quite like reading a post which will make individuals believe. Also, many thanks permitting me to comment!

  15. I think other web site proprietors should take this web site as an model, very clean and wonderful user friendly style and design, as well as the content. You are an expert in this topic!

  16. I don’t even know how I ended up here, but I thought this post was
    great. I do not know who you are but definitely you are going to a
    famous blogger if you are not already 😉 Cheers!

  17. you are really a good webmaster. The site loading speed is amazing. It seems that you are doing any unique trick. Also, The contents are masterpiece. you have done a magnificent job on this topic!

  18. It as really a nice and helpful piece of info. I am glad that you shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

  19. I’m really enjoying the theme/design of your site.

    Do you ever run into any internet browser compatibility problems?
    A handful of my blog visitors have complained about my website not working
    correctly in Explorer but looks great in Chrome. Do you have any tips to
    help fix this problem?

  20. I’m gone to inform my little brother, that he should also
    pay a quick visit this web site on regular basis to obtain updated from hottest information.

  21. Hey there! Do you know if they make any plugins
    to help with Search Engine Optimization? I’m trying
    to get my blog to rank for some targeted keywords but I’m not seeing very good results.
    If you know of any please share. Cheers!

  22. Howdy! I understand this is kind of off-topic but
    I had to ask. Does managing a well-established blog
    like yours take a large amount of work? I am brand new to blogging however
    I do write in my journal everyday. I’d like to start a blog so I can easily share my
    own experience and thoughts online. Please let me know if you have any kind of recommendations
    or tips for new aspiring blog owners. Appreciate it!

  23. You made some really good points there. I checked on the internet to find out more about the issue and found most individuals will go along with your views on this website.

  24. I will immediately seize your rss feed as I can not in finding your email subscription link or newsletter service. Do you ave any? Kindly permit me know so that I may subscribe. Thanks.

  25. Magnificent beat ! I would like to apprentice while you amend yoursite, how could i subscribe for a blog website? The account helpedme a applicable deal. I had been tiny bit acquainted of this yourbroadcast offered shiny clear idea

  26. Wow, fantastic blog structure! How long have you been running a blog for? you make running a blog glance easy. The total look of your web site is great, let alone the content!

  27. So pleased to possess found this publish.. Respect the admission you presented.. Undoubtedly handy perception, thanks for sharing with us.. So content to have identified this publish..

  28. My brother suggested I might like this website. He was totally right. This post truly made my day. You can not imagine simply how much time I had spent for this information! Thanks!

  29. Normally I do not read post on blogs, however I would like to say that this write-up very pressured me to check out and do so! Your writing taste has been surprised me. Thank you, quite nice post.

  30. This particular blog is really interesting and besides factual. I have picked helluva useful stuff out of this source. I ad love to go back again and again. Cheers!

  31. This very blog is without a doubt awesome and besides diverting. I have picked helluva helpful advices out of it. I ad love to return every once in a while. Cheers!

  32. Im no professional, but I believe you just made an excellent point. You obviously know what youre talking about, and I can actually get behind that. Thanks for staying so upfront and so honest.

  33. Wow, wonderful blog structure! How lengthy have you ever been blogging for? you made blogging look easy. The total glance of your website is great, let alone the content material!

THOÁT KHỎI BÌNH LUẬN

Please enter your comment!
Please enter your name here