Làm cách nào để chạy truy vấn mysql?

mysql_query doesnt support multiple queries, a way round this is to use innodb and transactions

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------

class MySQLDB
{
   private $connection;          // The MySQL database connection

________số 8

   /* Transactions functions */

ERROR 1049 (42000): Unknown database 'somedb'
0

ERROR 1049 (42000): Unknown database 'somedb'
1

ERROR 1049 (42000): Unknown database 'somedb'
2

ERROR 1049 (42000): Unknown database 'somedb'
3

ERROR 1049 (42000): Unknown database 'somedb'
4

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
0

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
1

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
2

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
3

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
4

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
5

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
6

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
7

this db class/function will accept an array of arrays of querys, it will auto check every line for affected rows in db, if one is 0 it will rollback and return false, else it will commit and return true, the call to the function is simple and is easy to read etc
----------
8

Thực thi các truy vấn MySQL trong Linux là một kỹ năng cần thiết cho các nhà phát triển back-end hoặc DevOps. Mặc dù có rất nhiều ứng dụng khách GUI để xử lý cơ sở dữ liệu, nhưng một kịch bản phổ biến là nhà phát triển thích chạy một số truy vấn trực tiếp trên thiết bị đầu cuối Linux. Chưa kể rằng đôi khi nhà phát triển có thể cần viết một tập lệnh bash…

Tiện ích dòng lệnh MySQL cho phép bạn chạy truy vấn và xem kết quả truy vấn, v.v. từ dấu nhắc lệnh tương tác. Cũng có thể chạy một truy vấn từ dòng lệnh mà không thực sự đi vào dấu nhắc tương tác. Bài đăng này xem xét làm thế nào để làm điều này

Ví dụ: tôi cần tải một số dữ liệu vào cơ sở dữ liệu mới vào một ngày khác từ kết xuất từ ​​​​máy chủ khác. Thông thường tôi muốn làm một cái gì đó như thế này

mysql -u [username] -p somedb < somedb.sql

Cơ sở dữ liệu không thực sự tồn tại nên tôi gặp lỗi này

ERROR 1049 (42000): Unknown database 'somedb'

Giải pháp rõ ràng là tạo cơ sở dữ liệu và sau đó chạy cùng một lệnh để tải dữ liệu. Tôi có thể làm điều này bằng cách thêm "tạo cơ sở dữ liệu somedb" vào đầu tệp văn bản mà tôi đang tải, kích hoạt dòng lệnh MySQL, chạy lệnh và thoát trở lại trình bao bash hoặc sử dụng cờ -e để thực thi

Vì vậy, để chạy một truy vấn MySQL duy nhất từ ​​trình bao thông thường của bạn thay vì từ dòng lệnh tương tác của MySQL, bạn sẽ làm điều này

mysql -u [username] -p [dbname] -e [query]

Trong trường hợp của tôi, tôi muốn tạo cơ sở dữ liệu để nó trông như thế này (lưu ý rằng tôi không cần chỉ định cơ sở dữ liệu vì truy vấn của tôi không ảnh hưởng đến cơ sở dữ liệu cụ thể)

mysql -u [username] -p -e "create database somedb"

Bạn có thể chạy bất kỳ truy vấn hợp lệ nào đối với bất kỳ cơ sở dữ liệu nào mà bạn có quyền, giống như cách chạy truy vấn từ dòng lệnh MySQL. Mọi đầu ra sẽ xuất hiện trên dòng lệnh shell của bạn. Ví dụ

 $ mysql -u root -p somedb -e "select * from mytable"
Enter password:
+------------+-------------+----------------------------+
| mytable_id | category_id | name                       |
+------------+-------------+----------------------------+
|          1 |           1 | Lorem ipsum dolor sit amet |
|          2 |           1 | Ut purus est               |
|          3 |           2 | Leo sed condimentum semper |
|          4 |           2 | Donec velit neque          |
|          5 |           3 | Maecenas ullamcorper       |
+------------+-------------+----------------------------+

Các truy vấn cập nhật và chèn không xuất ra bất cứ thứ gì nếu chúng thành công (hiển thị lỗi nếu không thành công), nhưng các truy vấn chọn thực hiện như trong ví dụ trên