Manual talk:Database access

Thanks for starting this page.

Not very well described
Where does it say what does "fname" or "options" or "vars" mean?
 * I believe those are described here. Tisane 20:11, 26 February 2010 (UTC)
 * I agree like almost all wikipedia technical and mediawiki articles, this article is terribly written, and difficult to understand for those who are not computer programmers. Errectstapler 14:03, 4 May 2011 (UTC)
 * I added a much needed explanation of how to login to mysql. Errectstapler 18:24, 4 May 2011 (UTC)

The information was reverted, here it is for others:

Errectstapler 19:56, 4 May 2011 (UTC)

SQL Injection
How safe are these functions against SQL Injection? Does the extension-coder have to take care of this themselves?
 * If you feed an array to select, it's safe. If you construct a string, even a string to be passed to ::select, then it's up to you to take care of safety. Gigs 03:07, 30 December 2010 (UTC)

select-statement
Where would I find documentation on the select-statement (no, not sql in general)?

I would assume:

$res = $dbr->select(Tables, Fields returned, WHERE-clause, calling function, options);


 * Tables:array of tables, if more then on, where-clause needs a join Example: array('pagelinks', 'page') or just 'page'
 * Fields returned: array of fields returned Example: 'pl_title'
 * WHERE-clause: array which holds all conditions, will be joined with AND (And how do I OR clauses?) Example: array('page_id = pl_from', 'pl_namespace' => NS_TEMPLATE, 'page_namespace' => $disPageObj->getNamespace, 'page_title' => $disPageObj->getDBkey)
 * calling function: just for the fun of it? Example: 'DisambiguationsPage::getSQL'
 * options: seems to be added after the WHERE clause, like LIMIT or ORDER BY

Full example: $res = $dbr->select(array('pagelinks', 'page'),	 	    'pl_title',	             array('page_id = pl_from', 'pl_namespace' => NS_TEMPLATE, 'page_namespace' => $disPageObj->getNamespace, 'page_title' => $disPageObj->getDBkey),		   'DisambiguationsPage::getSQL' );


 * The WHERE clause is hard coded to use AND and only do equality when you give it an array. An important caveat is that if you give a string instead of an array, you have to addQuotes and do all your sanitation manually.  I have updated the page accordingly. Gigs 03:02, 30 December 2010 (UTC)

How do I make queries with OR
Is this possible?
 * See code of  in , something like:

--Andreas P. 13:02, 23 January 2013 (UTC)

immediateBegin vs. begin
This document suggests using immediateBegin over begin, as begin 'may not do what you expect'. Same for immediateCommit vs. commit. However, the MW source code specifies that both immediateBegin and begin do the same thing, and that immediateBegin is deprecated in favor of begin. Same for commit. Unless anyone objects, I'm going to change the page to state that begin and commit should be used, and that the immediateX have been deprecated. --Msul01 20:50, 12 December 2008 (UTC)

ORDER BY and GROUP BY
Hi, i want to roder an group my query but it doesn't work. here my source $res = $dbr->select(                                       'flaggedrevs',                                        array( 'fr_page_id', 'fr_user' , 'fr_timestamp' , 'fr_comment' , 'fr_quality' ),                                        ,                                        ,                                        'GROUP BY fr_page_id ORDER BY fr_timestamp DESC'                );

the query it self is ok, but it seems like the order and group option does not work. hre the raw query from the cli

 select fr_timestamp,fr_user,fr_page_id,fr_quality from flaggedrevs group by fr_page_id order by fr_timestamp DESC; 

it works perfectly there. is there another way to query the database? This API seems to be not very efficient. --213.214.18.64 18:06, 20 January 2010 (UTC)


 * Max Semenik 18:34, 20 January 2010 (UTC)
 * thank you --213.214.18.64 16:29, 21 January 2010 (UTC)

CREATE TABLE
There should be a wrapper for CREATE TABLE. (Other than a duplicate of an existing table) Tisane 07:05, 12 March 2010 (UTC)

$res - and then?
There should be more documentation about what to do with the result of a query. I found a pointer to DatabaseType::fetchObject($res), but do I need to use it? What if there will always only be one result row? --Patrick Nagel 04:06, 4 May 2011 (UTC)
 * It is mentioned at the very beginning: foreach( $res as $row ) { ... }. Max Semenik 04:53, 4 May 2011 (UTC)
 * If there is one result row always use selectRow instead --Nischayn22 (talk) 12:19, 16 May 2012 (UTC)

Needed: Structure and better examples/explanation
This whole thing needs better structure and organisation. As it is it seems quite... all over the place, really. If I could be more specific, however, I could probably fix it myself, but unfortunately my sheer lack of comprehension precludes this. That, and bats are eating my legs.

In particular with the database query wrapper functions, much more is also needed to actually explain what's going on. The select example is helpful, but nowhere near enough to give an idea what all can be done with the things or how, nor is it clear if the listed ones are even all of them, considering how the last has the contents commented out? Quite possibly there should be an entire separate page for these, however, as with each parameter there are different formats and options, not all of which parallel what would be expected from the SQL. For instance, would there even be any way to do 'ORDER BY 0,5' given that ORDER BY apparently only takes numbers? But that it only does take numbers is the sort of information a user needs, and that other things take arrays, and what all things take arrays... -— Isarra ༆ 17:01, 22 November 2012 (UTC)
 * Or was that LIMIT? I can't remember. -— Isarra ༆ 17:03, 22 November 2012 (UTC)
 * I agree, the DB access functions could be much better documented (not to mention are a common source of confusion when people first start developing MediaWiki).
 * In regards to LIMIT. Different DB's handle how offsets are specified differently. In the MW db access functions we use a separate (fake) option called OFFSET to specify this. There is a (very complex) example in intersection extension. There are few examples of offset in MediaWiki, as generally using an offset paramter (instead of doing offset in the where cluase by saying something along the line AND SOME_FIELD > SOME_NUMBER) is rather inefficient. For example, the code would look like:

$dbr = wfGetDB( DB_SLAVE ); $res = $dbr->select(       'category',                                   // $table        array( 'cat_title', 'cat_pages' ),            // $vars (columns of the table)        'cat_pages > 0',                              // $conds        __METHOD__,                                   // $fname = 'Database::select',        array( 'ORDER BY' => 'cat_title ASC', 'LIMIT' => 10, 'OFFSET' => 5 )       ); Which would produce something like: SELECT cat_title, cat_pages FROM category WHERE cat_pages > 0 ORDER BY cat_title ASC LIMIT 5, 10; (And yes, I recognize there is absolutely no way to figure that out unless somebody tells you :S) Bawolff (talk) 22:21, 22 November 2012 (UTC)
 * Aye, that's exactly my point. But you having put out this example, there's now that much more for folks to go off... hopefully someone will eventually do a more thorough introduction, but this is still something, so thank you. -— Isarra ༆ 04:49, 23 November 2012 (UTC)

Distinct
is there a way to add DISTINCT to select? --Hiong3-eng5 (talk) 07:29, 5 August 2013 (UTC)


 * never mind, I got it. use as ...

$cond[] = 'DISTINCT'; --Hiong3-eng5 (talk) 08:20, 5 August 2013 (UTC)

Choosing whether to use the master vs. slave
The manual page says to use the master for "read queries that need to have absolutely newest information". Does anyone have any tips on what kinds of situations require we be that particular about having the newest info, and what kinds of situations we can afford to have some lag in? Is the master to be read from pretty much whenever you plan on writing new data to the database based on something you read from it? Leucosticte (talk) 22:57, 13 December 2013 (UTC)

How can I work with two tables with the same column name?
I try to solve the following problem:

Table 1: 'page'

page_id

page_title

page_namepsace

...

Table 2: 'page_ext'

page_id

page_date

...

The SQL string:

SELECT page.page_id as page_id, page_ext.page_id as page_id2 page.page_title as page_title, FROM page, page_ext WHERE page.page_id=page_ext.page_id'

How can I do it with the array syntax?

--Mchansy (talk) 13:09, 27 January 2015 (UTC)

This might help:

array( 'a' => 'user' )

This includes the user table in the query, with the alias "a" available for use in field names (e.g. a.user_name).

Reference: https://doc.wikimedia.org/mediawiki-core/master/php/classDatabaseBase.html

How to select count from database
Hello,

I will like to select the most number of page_id in database by using COUNT. But I am not sure of the syntax used. Any advise on this? What I have tried

private function getMostLikesArticle {			$dbr = wfGetDB( DB_SLAVE ); $res = $dbr->select(			$this->tableName,                               			array(COUNT(page_id),'page_id', 'user_id'),			array('page_id ' => $this->page->getId),					//'user_id > 0',                             			__METHOD__                                   				); $output = ''; foreach( $res as $row ) { $output .= 'Page ID '. $row->page_id. ' has '. $row->user_id. " entries.\n" ; echo "output is ". $output. ' ';		} }