简体   繁体   中英

How to scroll the result of PHP-Postgres SELECT FROM query?

I call a simple PHP-Postgres database lookup function from jQuery. I use a SELECT FROM query in Postgres. I want to verify that the input password matches the password in the record and return yes or no to jQuery. For that, I need to scroll through the result of the query.

Here's the part of the PHP program that does the query:

// Query the database

    $email_field = $_POST['email_field'];
    $password = $_POST['password'];

try {

$email_field = $_POST['email_field'];
$password = $_POST['password'];

$data = [
    'email_field' => $email_field,
];

$sql = "select * from tbl01 where email=:email_field";

$stmt= $pdo->prepare($sql,[PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL]);
$stmt->execute($data);

echo "Statement selected";

print_r($stmt->errorInfo());

$no=$stmt->rowCount();
echo " No of records = ".$no;
echo PHP_EOL;

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { 
   echo $row;
   echo PHP_EOL; }

} catch (PDOException $e) {
    error_log($e->getMessage());
}

$pdo = null;

?>

The lines above execute correctly. The echo "Statement selected"; shows in the dev console, but no echo from:

while($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row; }

So my question is how do I retrieve the rows (one by one) from this query? I know there is one row in the table that matches the query I am sending because I can see it on the Putty command line when I issue the query directly.

I think this is an elementary question, but I haven't found the answer.

Thanks for any help.

EDIT: per comment from Nick (below), this is the error returned by print_r($stmt->errorInfo());

Statement selectedArray
(
    [0] => 42703
    [1] => 7
    [2] => ERROR:  column "email_field" does not exist
LINE 1: ...SOR WITH HOLD FOR select * from tbl01 where email=email_fiel...
                                                        ^
)

Latest message, finally it succeeded thanks to Nick (see below):

Array
(
    [0] => 00000
    [1] => 
    [2] => 
)
1
Array
(
    [date] => 2019-08-27
    [0] => 2019-08-27
    [email] => janis@joplin.com
    [1] => janis@joplin.com
    [firstname] => Janis
    [2] => Janis
    [lastname] => Joplin
    [3] => Joplin
    [password] => pwdph
    [4] => pwdph
    [comments] => pwdph
    [5] => pwdph
    [sendupdates] => Yes
    [6] => Yes
)

There's a problem with your query string, you are not declaring the parameter correctly, it should be :email_field :

$sql = "select * from tbl01 where email=:email_field";

Also you have too many variables in the array being passed to execute . Only values actually used in the query should be in there, so remove the password value.

Note also that $row in while($row = $stmt->fetch(PDO::FETCH_ASSOC)) is an array, so you will need to print_r or var_dump it, echo $row; will simply output Array .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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