使用`$ row` [duplicate]从数据库中选择数据

时间:2021-03-25 19:26:11

This question already has an answer here:

这个问题在这里已有答案:

Here I am calling some data from database but something seems to have gone wrong, though no error is displayed but data isn't selected I used echo to test nothing came out, data is their in database but this statement doesn't select it.

这里我从数据库调用一些数据,但似乎出现了问题,虽然没有显示错误,但没有选择数据我使用echo来测试没有出现,数据是他们在数据库中但是这个语句没有选择它。

$que =$db->prepare("SELECT first_name, last_name, bio FROM userss WHERE username=:username");
$que->execute(array(':username'=>$username));
$row = $que->fetch(PDO::FETCH_ASSOC);
$db_first_name = $row['first_name'];
$db_last_name = $row['last_name'];
$db_bio = $row['bio'];

1 个解决方案

#1


0  

To know for sure you are not getting any errors, you have to look for them

要确定您没有收到任何错误,您必须寻找它们

I assume you have not set the connection to throw exceptions so if you add some error checking like this you will know for sure there are no errors, or what they are if they happen.

我假设您没有设置连接以抛出异常,因此如果您添加一些错误检查,您将确定没有错误,或者如果它们发生了它们是什么。

$que =$db->prepare("SELECT first_name, last_name, bio 
                    FROM userss WHERE username=:username");
if ( ! $que ) {
    print_r($db->errorInfo());
    exit;
}

$que->execute(array(':username'=>$username));
if ( ! $que ) {
    print_r($que->errorInfo();
    exit;
}

$row = $que->fetch(PDO::FETCH_ASSOC);
$db_first_name = $row['first_name'];
$db_last_name = $row['last_name'];
$db_bio = $row['bio'];

#1


0  

To know for sure you are not getting any errors, you have to look for them

要确定您没有收到任何错误,您必须寻找它们

I assume you have not set the connection to throw exceptions so if you add some error checking like this you will know for sure there are no errors, or what they are if they happen.

我假设您没有设置连接以抛出异常,因此如果您添加一些错误检查,您将确定没有错误,或者如果它们发生了它们是什么。

$que =$db->prepare("SELECT first_name, last_name, bio 
                    FROM userss WHERE username=:username");
if ( ! $que ) {
    print_r($db->errorInfo());
    exit;
}

$que->execute(array(':username'=>$username));
if ( ! $que ) {
    print_r($que->errorInfo();
    exit;
}

$row = $que->fetch(PDO::FETCH_ASSOC);
$db_first_name = $row['first_name'];
$db_last_name = $row['last_name'];
$db_bio = $row['bio'];