Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 带有PDO的select语句无法正常工作_Php_Mysql_Sql_Pdo - Fatal编程技术网

Php 带有PDO的select语句无法正常工作

Php 带有PDO的select语句无法正常工作,php,mysql,sql,pdo,Php,Mysql,Sql,Pdo,在这里,我将登录会话存储在$userId变量中,以获取我的id: $ userId = $_SESSION['result']['id']; 如果用户想要搜索某个名称,我将在此处存储一个$_会话['where']: if(isset($_POST['sendFiltro'])){ $search = $_POST['search']; if(!empty($search) && $search != 'Name:'){

在这里,我将登录会话存储在$userId变量中,以获取我的id:

$ userId = $_SESSION['result']['id'];
如果用户想要搜索某个名称,我将在此处存储一个$_会话['where']:

 if(isset($_POST['sendFiltro'])){
            $search = $_POST['search'];
            if(!empty($search) && $search != 'Name:'){
                $_SESSION['where'] = "AND name LIKE '%$search%'";
                header('Location: index2.php?exe=adminis/admins');
            }else{
                unset($_SESSION['where']);
                header('Location: index2.php?exe=adminis/adminis');
            }
        }
在这里,我执行select语句以使我的管理员在表中显示:

$pag = (empty($_GET['pag']) ? '1' : $_GET['pag']);
$max = 10;
$first = ($pag * $max) - $first;
$where = isset($_SESSION['where']) ? $_SESSION['where'] : '';
$readAdmin = $pdo->prepare("SELECT * from admins WHERE id != :id {$where} ORDER BY name ASC, level ASC LIMIT :first, :max");
$readAdmin->bindParam(':id', $userId, PDO::PARAM_INT);
$readAdmin->bindParam(':first', $first, PDO::PARAM_INT);
$readAdmin->bindParam(':max', $max, PDO::PARAM_INT);  
$readAdmin->execute();
$readAdminRows = $readAdmin->rowCount();
 if(!$readAdminRows)
    {
       echo 'There are no recors in admins table';
    }
    else
   {
    //I show my table
    }
我的Admins表中有20个记录,它总是说我的admin表中没有记录

你看到我的代码中出现了一些错误吗

如果我从我的sql语句
WHERE id!=中删除此项:id
它可以工作,但我不想在表中显示当前管理员及其会话

$readAdminRows = $readAdmin->rowCount();
如果将PDO配置为使用无缓冲查询,则rowCount()函数始终返回0:

$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

我建议您尝试获取结果集的第一行,如果第一次获取仍然没有返回任何内容,那么您就没有匹配项。

为什么是{$where}而不是“$where”?您应该对所有内容使用占位符,而不仅仅是某些内容<代码>$where在此处完全不显示。你真的允许从会话中任意注入SQL吗?@MrJack就是这样工作的。为什么这么多人不知道呢?谢谢你的观点,塔德曼,但我不知道其他的方法来做到这一点@我们中的一些人在PHP之前就学会了编写其他语言的代码;)