值没有从PHP数据库返回

时间:2022-12-11 19:23:41

new here and I am at my wits end with this problem. The pretence is simple; I am trying to select a user id from the database BASED on a value from a session cookie. I have confirmed that the SQL works as I have run it through sql itself, and the only thing I can think of is that it's not executing or my fetch or bind is wrong.

在这里新的,我在这个问题的最后。假装很简单;我试图根据会话cookie中的值从数据库中选择用户ID。我已经确认SQL正常运行,因为我已经通过sql本身运行它,我唯一能想到的是它没有执行或者我的fetch或bind是错误的。

<?php
session_start();
$cart = $_SESSION["cart"];

try{
require "connect.php";
$id = "SELECT id_no FROM eCustomer WHERE surname = :uid";
echo $id . '<br />';

$idfetch=$dbh->prepare($id);
$idfetch->bindValue(":uid", $_SESSION["uid"]);
$idfetch->execute();

$customer_id = $idfetch["id_no"];

echo $customer_id;

$dbh=null;

} catch (PDOExeption $e){
   echo $e;
}
?>

This should echo to my browser

这应该回应我的浏览器

SELECT id_no FROM eCustomer WHERE surname = :uid
1

But it's only outputting

但它只是输出

SELECT id_no FROM eCustomer WHERE surname = :uid

I have also tried putting the session variable directly into the sql statement without the bindValue

我还尝试将session变量直接放入没有bindValue的sql语句中

$id = "SELECT id_no FROM eCustomer WHERE surname = '" . $_SESSION["uid"] . "'";

Which outputs to my browser

哪个输出到我的浏览器

SELECT id_no FROM eCustomer WHERE surname = 'Mclellan'

(Mclellan being in my session cookie)

(Mclellan在我的会话cookie中)

Any help would be greatly appreciated.

任何帮助将不胜感激。

2 个解决方案

#1


You have actually executed the statement, but you haven't actually ->fetch()ed it yet. You missed that part:

你实际上已经执行了该语句,但实际上你还没有 - > fetch()编辑它。你错过了那部分:

$results = $idfetch->fetchAll(PDO::FETCH_ASSOC);
// print_r($results);
foreach($results as $row) {
    echo $row['id_no'];
}

Sidenote:

If you only require one row, then a single ->fetch() invocation should suffice:

如果您只需要一行,那么单个 - > fetch()调用就足够了:

$row = $idfetch->fetch(PDO::FETCH_ASSOC);
echo $row['id_no'];

#2


It looks like you are missing the actual fetching of the results

看起来你错过了实际的结果提取

you would need a line like below following your $idfetch->execute();

在$ idfetch-> execute()之后你需要一行如下所示;

$customerid = $idfetch->fetch(PDO::FETCH_ASSOC);

after that your echo $customerid["id_no"]; should work.

之后你的echo $ customerid [“id_no”];应该管用。

The reason being is that all you have had php do is execute the query without telling it to fetch the results from the query.

原因是所有你有php做的就是执行查询而不告诉它从查询中获取结果。

Also, the above is for using PDO as that is what it looks like you are using. If you are using mysqli then this probably wont help.

此外,以上是使用PDO,因为它是你正在使用的。如果您使用mysqli,那么这可能不会有帮助。

#1


You have actually executed the statement, but you haven't actually ->fetch()ed it yet. You missed that part:

你实际上已经执行了该语句,但实际上你还没有 - > fetch()编辑它。你错过了那部分:

$results = $idfetch->fetchAll(PDO::FETCH_ASSOC);
// print_r($results);
foreach($results as $row) {
    echo $row['id_no'];
}

Sidenote:

If you only require one row, then a single ->fetch() invocation should suffice:

如果您只需要一行,那么单个 - > fetch()调用就足够了:

$row = $idfetch->fetch(PDO::FETCH_ASSOC);
echo $row['id_no'];

#2


It looks like you are missing the actual fetching of the results

看起来你错过了实际的结果提取

you would need a line like below following your $idfetch->execute();

在$ idfetch-> execute()之后你需要一行如下所示;

$customerid = $idfetch->fetch(PDO::FETCH_ASSOC);

after that your echo $customerid["id_no"]; should work.

之后你的echo $ customerid [“id_no”];应该管用。

The reason being is that all you have had php do is execute the query without telling it to fetch the results from the query.

原因是所有你有php做的就是执行查询而不告诉它从查询中获取结果。

Also, the above is for using PDO as that is what it looks like you are using. If you are using mysqli then this probably wont help.

此外,以上是使用PDO,因为它是你正在使用的。如果您使用mysqli,那么这可能不会有帮助。