Trước khi làm một dự án ban phải phân tích và thiết kế CSDL dựa vào yêu cầu của khách hàng nên có lẽ bài này là hơi dư thừa, nhưng đôi lúc bạn cũng cần tạo database bằng code PHP đấy. Điển hình nhất là WordPress, khi bạn cài đặt lần đầu tiên nó sẽ tự động tạo ra database và table để tương thích với hệ thống CMS của nó.

Nội dung chính

  • 1. Tạo database bằng code PHP
  • Sử dụng MySQLi Object-oriented:
  • Sử dụng MySQLi Procedural:
  • Sử dụng PDO:
  • 2. Lời kết
  1. Tạo database bằng code PHP

Trước khi tạo database thì ban phải kết nối CSDL trước, sau đó viết câu truy vấn tạo Database và thực thi câu truy vấn đó bằng PHP.

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

20

// Tạo kết nối

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

 

// Nếu kết nối thất bại

if ($conn->connect_error) {

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

}

 

// Lệnh tạo database

$sql = “CREATE DATABASE FreettusDemo”;

 

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

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

echo “Tạo database thành công”;

} else {

echo “Tạo database thất bại: ” . $conn->error;

}

 

// Tạo xong thì ngắt kết nối

$conn->close();

Lưu ý: Khi bạn sử dụng lệnh tạo database thì bạn chỉ kết nối vào MySQL và không được chọn database, nghĩa là bạn chỉ được phép truyền vào ba tham số host, username và password. Trường hợp bạn có sử dụng port thì bạn phải truyền tham số port vào, nhưng tham số tên database thì bạn truyền giá trị rỗng mysqli(“localhost”, “username”, “password”, “”, port).

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

// Tạo kết nối

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

 

// Nếu kết nối thất bại

if (!$conn) {

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

}

 

// Lệnh tạo database

$sql = “CREATE DATABASE FreettusDemo”;

 

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

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

echo “Tạo database thành công”;

} else {

echo “Tạo database thất bại: ” . mysqli_error($conn);

}

 

// Tạo xong thì ngắt kết nối

mysqli_close($conn);

Sử dụng PDO:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

try {

// Chuỗi kết nối

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

 

// Thiết lập chế độ exception

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

 

// Câu truy vấn

$sql = “CREATE DATABASE FreetutsDemo”;

 

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

$conn->exec($sql);

 

// Thông báo thành công

echo “Tạo database thành công”;

}

catch(PDOException $e)

{

echo $e->getMessage();

}

 

// Ngắt kết nối

$conn = null;

Trường hợp này có một vấn đề như ở bài trước mình có trình bày đó là bạn phải truyền tên database vào chuỗi kết nối. Nhưng với trường hợp này thì ta không cần truyền tên database vào bởi vì ta đang sử dụng lệnh tạo database.

  1. Lời kết

Như vậy là mình đã giới thiệu xong ba cách tạo database bằng code PHP đơn giản nhất bằng hai thư viện PDO và MySQLi. Bài tiếp theo chúng ta sẽ tìm hiểu cách tạo database table bằng PHP.

304 BÌNH LUẬN

  1. Wow! This can be one particular of the most useful blogs We ave ever arrive across on this subject. Actually Magnificent. I am also a specialist in this topic so I can understand your effort.

  2. I thought it was going to be some boring old post, but it really compensated for my time. I will publish a link to this page on my blog site. I am confident my visitors will locate that really useful

  3. I see in my blog trackers significant traffic coming from facebook. My blog is not connected with facebook, I don at have an account there, and I can at see, who posts the linksany ideas?.

  4. Wow! This could be one particular of the most helpful blogs We have ever arrive across on this subject. Basically Magnificent. I am also a specialist in this topic so I can understand your effort.

  5. This very blog is obviously entertaining as well as amusing. I have picked helluva interesting things out of this blog. I ad love to go back again and again. Thanks a lot!

  6. You ave made some good points there. I looked on the internet for more information about the issue and found most individuals will go along with your views on this site.

  7. Your style is unique in comparison to other people I ave read stuff from. Many thanks for posting when you have the opportunity, Guess I all just book mark this site.

  8. Thank you, I have just been searching for information about this subject for ages and yours is the best I ave discovered till now. But, what about the conclusion? Are you sure about the source?

  9. magnificent issues altogether, you just won a brand new reader. What might you suggest in regards to your publish that you just made a few days in the past? Any certain?

  10. Wow! This can be one particular of the most beneficial blogs We ave ever arrive across on this subject. Basically Excellent. I am also an expert in this topic so I can understand your effort.

  11. Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is fantastic, as well as the content!. Thanks For Your article about sex.

  12. Nice blog here! Also your web site loads up fast! What host are you using? Can I get your affiliate link to your host? I wish my website loaded up as quickly as yours lol

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

  14. Wow! This could be one particular of the most beneficial blogs We have ever arrive across on this subject. Basically Fantastic. I am also an expert in this topic so I can understand your effort.

  15. Im no professional, but I believe you just crafted the best point. You clearly comprehend what youre talking about, and I can actually get behind that. Thanks for staying so upfront and so sincere.

  16. Thanks for another excellent article. Where else could anyone get that type of info in such an ideal way of writing? I ave a presentation next week, and I am on the look for such information.

  17. This is very interesting, You are a very skilled blogger. I have joined your rss feed and look forward to seeking more of your excellent post. Also, I have shared your site in my social networks!

  18. This awesome blog is definitely entertaining additionally amusing. I have chosen many handy tips out of this amazing blog. I ad love to return again and again. Thanks a bunch!

  19. Wow, incredible blog format! How lengthy have you been blogging for? you made running a blog look easy. The total glance of your site is fantastic, as well as the content material!

  20. You ave made some really good points there. I looked on the internet for more information about the issue and found most people will go along with your views on this web site.

  21. You could certainly see your skills in the work you write. The sector hopes for even more passionate writers such as you who are not afraid to say how they believe. Always go after your heart.

  22. Your style is so unique in comparison to other folks I have read stuff from. Thanks for posting when you ave got the opportunity, Guess I all just book mark this page.

  23. What as Going down i am new to this, I stumbled upon this I have found It positively useful and it has aided me out loads. I hope to contribute & aid other customers like its aided me. Good job.

  24. This is a really great study for me, Ought to admit that you just are a single of the best bloggers I ever saw.Thanks for posting this informative post.

  25. It is really a nice and helpful piece of info. I am glad that you shared this useful information with us. Please keep us up to date like this. Thank you for sharing.

  26. Wow! This can be one particular of the most useful blogs We have ever arrive across on this subject. Actually Wonderful. I am also a specialist in this topic so I can understand your effort.

  27. Simply a smiling visitor here to share the love (:, btw outstanding pattern. Make the most of your regrets. To regret deeply is to live afresh. by Henry David Thoreau.

  28. I thought it was going to be some boring old publish, but it really compensated for my time. I will publish a link to this page on my weblog. I am sure my visitors will find that really useful

  29. I simply could not leave your website before suggesting that I extremely enjoyed the standard info an individual supply to your guests? Is going to be again ceaselessly in order to inspect new posts.

  30. You could definitely see your skills in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always follow your heart.

  31. This is very interesting, You are a very skilled blogger. I ave joined your rss feed and look forward to seeking more of your excellent post. Also, I ave shared your web site in my social networks!

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

Please enter your comment!
Please enter your name here