简体   繁体   English

致命错误:未捕获错误:无法使用PDOStatement类型的对象作为数组

[英]Fatal error: Uncaught Error: Cannot use object of type PDOStatement as array

I want a php script that gives me a random selected row from my db table and change the value of "dispensered" to 1. Im sure its kinda stupid, but Im new to this stuff. 我想要一个php脚本,它从我的db表中给我一个随机选择的行,并将“allocatesered”的值更改为1.我确定它有点愚蠢,但我是这个东西的新手。

Here is my code: 这是我的代码:

<?php

$hostname = 'localhost';
$user = 'root';
$pass = '';
$database = 'testt';

$db_connection = new PDO( "mysql:host=" . $hostname . ";dbname=" . $database, 
$user, $pass );

$results = $db_connection->query( 'SELECT username, password FROM accounts WHERE dispensered = 0 ORDER by rand() LIMIT 1' );

$db_connection->query( 'UPDATE accounts SET dispensered=1 WHERE id='.$results['id'].'' );

foreach ( $results as $row ) {
    echo '<p id="username">' . $row['username'] . '</p>';
    echo '<p id="password">&ndash;' . $row['password'] . '</p>';
}




// Close the connection
$db_connection = null;

Best regards 最好的祝福

Fatal error: Uncaught Error: Cannot use object of type PDOStatement as 
array in C:\xampp\htdocs\test\index.php:13 Stack trace: #0 {main} thrown 
in C:\xampp\htdocs\test\index.php on line 13

EDIT: Tried that; 编辑:试过;

$test = $results;
$db_connection->query( 'UPDATE accounts SET dispensered=1 WHERE 
id='.$test['id'].'' );

But it didnt work aswell :( 但它没有工作,以及:(

PDO::query — Executes an SQL statement, returning a result set as a PDOStatement object. PDO :: query - 执行SQL语句,将结果集作为PDOStatement对象返回。 You're trying to access the $results as an array in your update query. 您正尝试在更新查询中将$ results作为数组访问。 Hence you're seeing this error. 因此,你会看到这个错误。 You need to capture the 'id' value from $row in the for loop and run your update query after this. 您需要从for循环中的$ row捕获'id'值,然后在此之后运行更新查询。

暂无
暂无

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

相关问题 致命错误:未被捕获的错误:不能将Entrada类型的对象用作数组 - Fatal error: Uncaught Error: Cannot use object of type Entrada as array FATAL ERROR未捕获错误:无法使用stdClass类型的对象作为数组 - FATAL ERROR Uncaught Error: Cannot use object of type stdClass as array 致命 Wordpress 错误:致命错误:未捕获错误:无法使用 WP_Error 类型的 object 作为数组 - Fatal Wordpress Error: Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in PHP 致命错误:未捕获的错误:无法使用 stdClass 类型的 object 作为数组 - PHP Fatal error: Uncaught Error: Cannot use object of type stdClass as array 致命错误:无法将类型的对象用作数组 - Fatal Error : Cannot use object of type as array 致命错误:未捕获错误:class PDOStatement 的 Object 无法转换为字符串 - Fatal error: Uncaught Error: Object of class PDOStatement could not be converted to string 不能使用PDOStatement类型的对象作为数组 - Cannot use object of type PDOStatement as array 不能使用 PDOStatement 类型的 object 作为数组 - Cannot use object of type PDOStatement as array in 致命错误:未捕获错误:无法在 C:\xampp\htdocs\MyShoppingList\list.php:487 中使用类型为 stdClass 的 object 作为数组 - Fatal error: Uncaught Error: Cannot use object of type stdClass as array in C:\xampp\htdocs\MyShoppingList\list.php:487 致命错误:不能将Stripe \\ Customer类型的对象用作数组 - Fatal error: Cannot use object of type Stripe\Customer as array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM