Truy vấn mysqli trả về null php

Mặc dù hầu hết các ứng dụng sẽ có một số dạng công nghệ phía máy chủ hoặc thậm chí phía máy khách có thể được sử dụng để lọc ra các kết quả truy vấn cơ sở dữ liệu có thể chứa giá trị

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3 hoặc trống, nhưng việc sử dụng ngôn ngữ khác và gánh nặng thực thi mã bổ sung đó thường tốn kém hơn cho

Thay vào đó, bên dưới chúng ta sẽ khám phá ngắn gọn cách sử dụng chính công cụ MySQL để thực hiện tác vụ này, chỉ lấy các bản ghi mà bạn mong muốn trong khi loại trừ những bản ghi có

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3 cột phiền phức trong hỗn hợp

IS NOT NULL Toán tử so sánh

Cho đến nay, phương pháp đơn giản và dễ hiểu nhất để đảm bảo tập kết quả của một cột cụ thể không chứa giá trị

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3 là sử dụng toán tử so sánh
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
6

Ví dụ: nếu chúng tôi muốn chọn tất cả các bản ghi trong bảng

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
7 trong đó cột
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
8 không phải là
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3, truy vấn có thể trông như thế này

SELECT
  primary_author,
  published_date,
  title
FROM
  books
WHERE
  primary_author IS NOT NULL;

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
6 hoạt động giống như bất kỳ bộ so sánh nào khác, nên nó có thể được kết hợp với các mệnh đề
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
1 khác để lọc thêm kết quả, giống như khi bạn đang sử dụng
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
2,
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3,
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
4,
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
5 và các toán tử so sánh tiêu chuẩn khác

Lọc NULL từ nhiều cột

Trong một số trường hợp, bạn có thể muốn truy xuất kết quả khi không có giá trị

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3 nào trên nhiều cột. Ví dụ: chúng tôi có một số bản ghi trong bảng
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
7

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000

Hãy lưu ý rằng The Doors of Stone (

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
8) chưa được xuất bản và do đó,
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
9 là
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3. Tương tự, Beowulf (
SELECT
  primary_author,
  published_date,
  title
FROM
  books
WHERE
  (
    primary_author IS NOT NULL
  AND
    published_date IS NOT NULL
  );
1) – thiên anh hùng ca cổ nổi tiếng của Anh – không rõ tác giả, vì vậy
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
8 là
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3

Trong trường hợp này, chúng tôi có thể muốn truy vấn các kết quả chỉ chứa bốn bản ghi đầu tiên, do đó loại trừ hai bản ghi cuối cùng có giá trị

id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
3 trong
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
8 hoặc
id | title                | primary_author  | published_date
1  | The Hobbit           | Tolkien         | 09-21-1937
2  | The Silmarillion     | Tolkien         | 09-15-1977
3  | The Name of the Wind | Rothfuss        | 03-27-2007
4  | The Wise Man's Fear  | Rothfuss        | 03-01-2011
5  | The Doors of Stone   | Rothfuss        | NULL
6  | Beowulf              | NULL            | 01-01-1000
9. Điều này có thể được thực hiện với câu lệnh đơn giản này bằng cách sử dụng
SELECT
  primary_author,
  published_date,
  title
FROM
  books
WHERE
  (
    primary_author IS NOT NULL
  AND
    published_date IS NOT NULL
  );
7 với nhiều toán tử so sánh

SELECT
  primary_author,
  published_date,
  title
FROM
  books
WHERE
  (
    primary_author IS NOT NULL
  AND
    published_date IS NOT NULL
  );

Ghi chú. Dấu ngoặc đơn không bắt buộc nhưng nên đặt các bộ so sánh được nhóm lại để dễ đọc hơn

Ở đó bạn có nó;

MySQLi là gì

Giá trị trả về ¶ . Đối với các truy vấn thành công khác, mysqli_query() sẽ trả về true. a mysqli_result object. For other successful queries, mysqli_query() will return true .

Mysqli_query trả về cái gì nếu trống?

Đối với các truy vấn thành công khác, mysqli_query() sẽ trả về TRUE. Số 0 là kết quả của một hàng . Cảm ơn bạn đã trả lời.

Làm cách nào để viết truy vấn MySQLi trong PHP?

php //Tạo kết nối $con = mysqli_connect("localhost", "root", "password", "mydb"); //Executing the multi query $query = "SELECT * FROM players"; //Retrieving the records $res = mysqli_query($con, $query, MYSQLI_USE_RESULT); if ($res) { while ($row = mysqli_fetch_row($res)) { print("Name: ".

Làm cách nào để truy xuất dữ liệu từ cơ sở dữ liệu trong PHP bằng MySQLi?

Kết nối với cơ sở dữ liệu. .
Hướng đối tượng MySQLi $conn = new mysqli($servername, $username, $databasename)
Thủ tục MySQLi $conn = mysqli_connect($servername, $username, $password, $databasename);
PDO. $conn = PDO mới("mysql. máy chủ=$servername;dbname=myDB", $username, $password, $databasename);