简体   繁体   English

在非对象中调用成员函数fetch()

[英]Call to a member function fetch() on a non-object in

I'm trying to make a select in php with pdo. 我正在尝试使用pdo在php中进行选择。
It's working with a lot of req but not when I'm using a like, example : 它可以处理很多需求,但是当我使用like时,例如:

Query: 查询:

SELECT * FROM projets WHERE `identifiantProjetRattachement` LIKE "%PC13287%" ORDER BY "identifiantProjetDSR"

This is my php code : 这是我的php代码:

<?php
    function requette($requette,$table)
    {
        $bdd = new PDO('mysql:host=localhost;dbname=spb', 'root', '');
        $sql =  "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table';";
        $req = $bdd->query($sql);

        $titres = array();

        while($resultat = $req->fetch())
        {
           array_push($titres,$resultat['COLUMN_NAME']);
        }

        // ON A LES TITRES DONC ON PASSE AU CONTENU
        $sql =  $requette;
        $req = $bdd->query($sql);

        $retour = "";
        $cpt = 0;
        $nbLigne = 0;

        while($resultat = $req->fetch())
        {
           $nbLigne++;
           $retour .= "<tr><td class='boutonTouteLaCase' onclick='surlignerLigneProjets(this);'></td><td>$nbLigne</td>";
           for($cpt=0;$cpt<count($titres);$cpt++)
           {
             if($titres[$cpt] != "id")
               $retour .= "<td>".utf8_encode($resultat[$titres[$cpt]])."</td>";
           }

           $retour .= "</tr>";
        }
        echo $retour;   
      }

    $requette = htmlentities($_POST['requette']);
    $table = htmlentities($_POST['table']);

    echo $requette."<br/>";
    echo $table."<br/>";

     echo requette($requette,$table);?>

Do you think it's an accent problem ? 您认为这是一个口音问题吗? Or a problem due to a ' ? 还是由于'而引起的问题?
PS : This req works on phpmyadmin. PS:此要求适用于phpmyadmin。
Thanks for all. 谢谢大家

The problem is that you are invalidating your own queries: 问题是您使自己的查询无效:

...

$requette = htmlentities($_POST['requette']);
$table = htmlentities($_POST['table']);

echo $requette."<br/>";
echo $table."<br/>";

echo requette($requette,$table);

Note that you are using htmlentities($requette) and later you try to run that exact query: 请注意,您正在使用htmlentities($requette) ,稍后尝试运行该确切查询:

$sql =  $requette;
$req = $bdd->query($sql);

And that query will look like: 该查询将类似于:

SELECT * FROM projets WHERE identifiantProjetRattachement
    LIKE &quot;%PC13287%&quot; ORDER BY &quot;identifiantProjetDSR&quot;

For the example that you mentioned. 对于您提到的示例。 An invalid query. 无效的查询。

So you should only use htmlentities() when you output your variable to the screen, but you should not change the variable itself: 因此,仅在将变量输出到屏幕时才应使用htmlentities() ,而不应更改变量本身:

// Don't do this:
// $requette = htmlentities($_POST['requette']);
// $table = htmlentities($_POST['table']);

// just do this:
echo htmlentities($requette)."<br/>";
echo htmlentities($table)."<br/>";

echo requette($requette,$table);

Apart from that I will assume that you only have access to this yourself as you are running user-provided sql queries directly so that would be very dangerous if an unauthorized user had access to that. 除此之外,我将假定您仅在直接运行用户提供的sql查询时才可以自己访问此权限,因此如果未经授权的用户可以访问它,那将是非常危险的。

In where condition use column name not table name TABLE_NAME='$table'; where情况下,请使用column name而不是table name TABLE_NAME='$table';

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='$table';

it would be 这将是

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME='$CONDITION';

Also ORDER BY COLUMN_NAME is ASC OR DESC 另外, ORDER BY COLUMN_NAME is ASC OR DESC

Check the doc of mysqli::query: http://php.net/manual/de/pdo.query.php 检查mysqli :: query的文档: http : //php.net/manual/de/pdo.query.php

It can return PDOStatement object (where fetch is possible) or FALSE on failure, so check, if there was an error in your sql query before trying to fetch any results. 它可以在失败时返回PDOStatement对象(可以进行提取)或FALSE,因此请在尝试获取任何结果之前检查sql查询中是否存在错误。

$req = $bdd->query($sql);
if (!$req) {
  return false;
  // maybe give some better error message
}

Call to a member function fetch() on a non-object in line what? 在非对象上调用成员函数fetch()的方式是什么?

That point will tell you exactly where you are doing something wrong. 该点将准确告诉您您在哪里做错了什么。 Ensure you are not overwriting your query string by assigning it to a string and not an Object as specified by PDO. 通过将查询字符串分配给字符串而不是PDO指定的对象,确保不覆盖查询字符串。 Another is to ensure that your $req->rowCount() returns a value and not null before you call your $req->fetch() 另一个方法是在调用$req->fetch()之前,确保$req->rowCount()返回一个值并且不为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM