A partial archive of https://discourse-mediawiki.wmflabs.org as of Saturday May 21, 2022.

How to get values from Database?

Jayprakash12345

Hello Everyone, I made locally SQL table in enwiki database called ‘mytable’, and It has 3 fields ‘id’, ‘userid’ and ‘status’. So How I can get ‘status’ value from database? and ‘userid’ will be given as input.

I have tried this.

function getStatus ( $userid )
{
                $dbr = wfGetDB( DB_REPLICA ); // Only for read, not write
		$row = $dbr->select(
			'mytable',
			[
				'status'
			],
			[
				'userid' => $userid
			],
			__METHOD__
		);
}

But on echo,

Recoverable fatal error: Object of class Wikimedia\Rdbms\ResultWrapper could not be converted to string in C:\xampp\htdocs\Dev\core\extensions\Blah\include\Blah.php on line 29

How I can get values in string?

LucasWerkmeisterWMDE

There’s a wrapper function for this simple case:

$status = $dbr->selectField( 'mytable', 'status', [ 'userid' => $userid ] );

More generally, if you use select you typically iterate over the ResultWrapper:

$result = $dbr->select( 'mytable', [ 'status' ], [ 'userid' => $userid ] );
$statuses = [];
foreach ( $result as $row ) {
    $statuses[] = $row->status;
}
Jayprakash12345

Ah, It is working Thank you very much. var_dump ($status); return string. Thanks