gpt4 book ai didi

php - 获取具有特定值的字段数? :mysql-php

转载 作者:行者123 更新时间:2023-11-29 20:40:05 27 4
gpt4 key购买 nike

我正在尝试从表中获取付款次数(PAID)。

Please Assume This is the table:

| book_no | name | mobile | date1 | date2 | date3 | date4 |.. daten |
|---------|------|--------|-------|-------|-------|-------|-------|
| 1 | Cell | | PAID | DUE | DUE | | |
| 2 | Cell | | PAID | PAID | PAID | | |
| 3 | Cell | | DUE | DUE | DUE | DUE | |
| 4 | Cell | | PAID | PAID | PAID | | |
| 5 | Cell | | DUE | DUE | DUE | | |

上表中 Count(Paid) = 7

日期列的数量是动态的,因此我认为搜索整个表并获取 PAID 计数更为明智。

这是我设法编写的关于堆栈溢出的引用答案的代码,但我认为它不适合这个

        //what is the search?
$search = "PAID";
//get all the columns

$columnsq ="SELECT
COLUMN_NAME
FROM
information_schema.COLUMNS
WHERE TABLE_NAME = " .$scheme_name. "
AND TABLE_SCHEMA = 'gold' ";

var_dump($columns);
//put each like clause in an array
$queryLikes = array();
while ($column = $columns->fetch_assoc()) {
$queryLikes[] = $column['COLUMN_NAME'] . " LIKE '%$search%'";
}

$query = "SELECT COUNT(*) FROM " .$scheme_name. " WHERE " . implode(" OR ", $queryLikes);
//echo $query; //should look like this:
//SELECT * FROM users WHERE column1 LIKE '%something%' OR column2 LIKE '%something%' OR column3 LIKE '%something%' OR ...
//so then
$users=mysqli_query($conn,$query);
while ($user = $users->fetch_assoc()) {
//do stuff with $user
echo $users;
}

当我尝试执行上述代码时出现此错误

                Notice: Undefined variable: columns in E:\xampp\htdocs\schemeTable11.php on line 391
NULL
Notice: Undefined variable: columns in E:\xampp\htdocs\schemeTable11.php on line 394

Fatal error: Uncaught Error: Call to a member function fetch_assoc() on null

因此我正在寻找替代解决方案。 请帮忙

最佳答案

您确实需要学习如何阅读错误消息:

$columnsq ="SELECT
^^----note those two letters

var_dump($columns);
^---UNDEFINED
//put each like clause in an array
$queryLikes = array();
while ($column = $columns->fetch_assoc()) {
^--UNDEFINED

您从未执行过查询,也从未定义过$columns,而这正是 PHP 试图告诉您的内容。

关于php - 获取具有特定值的字段数? :mysql-php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38663570/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com