Domanda

Il seguente blocco di codice funziona correttamente (nessun errore)

$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- This returns 'object'
$statement->bindParam(1, $username);

Quanto segue dà: Errore irreversibile: chiamata a una funzione membro bindParam () su un non oggetto in /file.php sulla linea 39

$email = 'fake@email.com';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
echo gettype($statement); // -- this returns 'boolean'
$statement->bindParam(1, $email); // -- this is line 39.

Ora questo è strano.

Sul mio computer locale e sul mio host remoto, questo non è mai stato un problema.

Questo errore si presenta solo su questa nuova società di hosting che sto provando per il mese. Potrebbe essere un parametro di configurazione quando hanno compilato php?

Modifica -------- -------- Mentre cercavo ancora di capire cosa non andava, l'ho scoperto.

$query = "select userID, username from users";
$statement = $sql->prepare($query);    
$statement->execute();
$r = $statement->fetchAll(PDO::FETCH_ASSOC);

// display # of rows
echo "Rows returned: " . $statement->rowCount();

// display results array
echo '<pre>'; print_r($r); echo '</pre>'; 

Su un server, ottengo

Rows returned: 4

Array
(
    [0] => Array
        (
            [userID] => 1
            [username] => lyrae
        )

    [1] => Array
        (
            [userID] => 2
            [username] => jproffer
        )

    [2] => Array
        (
            [userID] => 3
            [username] => king
        )

    [3] => Array
        (
            [userID] => 4
            [username] => gergy
        )

)

Che è corretto. Dice 4 righe restituite e visualizza l'array dei risultati. Su un altro server, tuttavia, ottengo

Rows returned: 0

Array
(
    [0] => Array
        (
            [userID] => 1
            [username] => lyrae
        )

    [1] => Array
        (
            [userID] => 2
            [username] => jproffer
        )

    [2] => Array
        (
            [userID] => 3
            [username] => king
        )

    [3] => Array
        (
            [userID] => 4
            [username] => gergy
        )

)

Quindi, sembra anche che PDOStatement :: rowCount () non funziona su un server ma funziona su un altro.

È stato utile?

Soluzione 2

Trovato una soluzione al problema.

Questo è l'intero blocco di codice ..

// check if username exists
$query = "select * from users where username = ?";
$statement = $sql->prepare($query);
$statement->bindParam(1, $username);
$statement->execute();


// check if email exists
$sql2 = new PDO('mysql:host=localhost; dbname=db', 'username', 'pw');
$query = "select * from users";
$statement = $sql2->prepare($query);
echo gettype($statement);
#$statement->bindParam(1, $email);

Quindi, per qualche motivo, devo creare una nuova istanza di DOP. la cosa strana è che su altri 2 server non devo farlo.

E guardando oltre, ho scoperto che DOP :: Prepare genera un'eccezione PDO.

Eccolo:

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.

Array
(
    [0] => HY000
    [1] => 2014
    [2] => Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
)

Forse aiuterà qualcuno in futuro :)

Altri suggerimenti

Leggi questo: $ statement- > closeCursor ()

  

PDOStatement :: closeCursor () libera la connessione al server in modo che possano essere emesse altre istruzioni SQL

Stai utilizzando lo stesso database sul server in cui dici di non avere questo problema?

$ email non è definito? Puoi provare anche var_dump ($ email) per vedere cosa dice. Buona fortuna.

Hai provato a mettere la riga $ email = sotto il bindParam (ma prima di eseguire l'esecuzione)? bindParam passa il parametro per riferimento in modo da poter eseguire come query, modificare il valore della variabile ed eseguire nuovamente.

Penso che probabilmente sia un errore di configurazione di PHP. Ho sentito gente dire che DOP aveva molti bug prima di PHP 5.3, quindi forse vedi se riesci a ottenere PHP fino all'ultima versione?

Hai anche provato a scambiare le due query? Forse qualcosa si rompe dopo aver eseguito una query.

Consiglio di usare questo:

$email = 'fake@email.com';
$query = "select * from users where email = ?";
$statement = $sql->prepare($query);
$statement->execute(array($email));

Non è necessario l'uso di BindParam qui.

Ho avuto lo stesso problema con   echo " Righe restituite: " . $ Statement- > rowCount ();

Ho -1 righe, lol. Il mio database è INFORMIX e ho cercato sul web che rowCount (); restituisce solo le righe interessate in un'istruzione DELETE, INSERT o UPDATE eseguita dall'oggetto PDOStatement corrispondente.

Con un'istruzione SELECT è necessario utilizzare la funzione   $ Statement- > fetchColumn ();

leggilo qui: http://www.phpbuilder.com /manual/en/function.pdostatement-rowcount.php

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top