https://bugzilla.wikimedia.org/show_bug.cgi?id=31534
Dmitriy Sintsov <[email protected]> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |[email protected] --- Comment #2 from Dmitriy Sintsov <[email protected]> 2011-10-24 07:08:41 UTC --- May I ask you to explain what's wrong with my code? I decided to convert manually built SQL strings with db->execute() to complex selects. The original source code fragment is: $qp_users_polls = self::$db->tableName( 'qp_users_polls' ); $qp_users = self::$db->tableName( 'qp_users' ); $query = "SELECT qup.uid AS uid, name AS username, short_interpretation, long_interpretation, structured_interpretation " . "FROM $qp_users_polls qup " . "INNER JOIN $qp_users qu ON qup.uid = qu.uid " . "WHERE pid = " . intval( $this->pid ) . " " . "LIMIT " . intval( $offset ) . ", " . intval( $limit ); $res = self::$db->query( $query, __METHOD__ ); $result = array(); while ( $row = self::$db->fetchObject( $res ) ) { $interpResult = new qp_InterpResult(); $interpResult->short = $row->short_interpretation; $interpResult->long = $row->long_interpretation; $interpResult->structured = $row->structured_interpretation; $result[intval( $row->uid )] = array( 'username' => $row->username, 'interpretation' => $interpResult ); } I've converted it to: $res = self::$db->select( array( 'qu' => 'qp_users', 'qup' => 'qp_users_polls' ), array( 'qup.uid AS uid', 'name AS username', 'short_interpretation', 'long_interpretation', 'structured_interpretation' ), /* WHERE */ 'pid = ' . intval( $this->pid ), __METHOD__, array( 'OFFSET' => $offset, 'LIMIT' => $limit ), /* JOIN */ array( 'qu' => array( 'INNER JOIN', 'qup.uid = qu.uid' ) ) ); $result = array(); foreach ( $res as $row ) { $interpResult = new qp_InterpResult(); $interpResult->short = $row->short_interpretation; $interpResult->long = $row->long_interpretation; $interpResult->structured = $row->structured_interpretation; $result[intval( $row->uid )] = array( 'username' => $row->username, 'interpretation' => $interpResult ); } which produces the following invalid query (in 1.17, my primary target for development): Обнаружена ошибка синтаксиса запроса к базе данных. Это может означать ошибку в программном обеспечении. Последний запрос к базе данных: SELECT qup.uid AS uid,name AS username,short_interpretation,long_interpretation,structured_interpretation FROM `wiki_qp_users_polls` 'qup' INNER JOIN `wiki_qp_users` 'qu' ON ((qup.uid = qu.uid)) WHERE pid = 7 LIMIT 20 произошёл из функции «qp_PollStore::pollVotersPager». База данных возвратила ошибку «1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''qup' INNER JOIN `wiki_qp_users` 'qu' ON ((qup.uid = qu.uid)) WHERE pid = 7 LI' at line 1 (127.0.0.1)». They Database class built query is identical to manually built query, except the Database class wraps table aliases into single quotes. Original query works. Also, there are two more questions: 1. Is it safe to do not intval() 'LIMIT' and 'OFFSET' options values? 2. What if $res returned by query is not iterable object, how bad foreach() will fail? Isn't Database::fetchObject() more error-prone? -- Configure bugmail: https://bugzilla.wikimedia.org/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- You are the assignee for the bug. You are on the CC list for the bug. _______________________________________________ Wikibugs-l mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/wikibugs-l
