aaxc Posted October 3, 2012 Report Posted October 3, 2012 Tātad, man ir izveidotā šāda funkcija <?php $pdo_db = new PDO( 'mysql:host=' . $db_host . ';dbname=' . $db_name, $db_user_name, $db_password ); function qlist_pdo( $fields, $tables, $where_fields='', $where_values='', $order='', $limit='' ) { GLOBAL $pdo_db; // ignore this, it will be in class later. This is just for test purposes if ( $order ) $order = ' ORDER BY ' . $order; if ( $limit ) $limit = ' LIMIT ' . $limit; if ( is_array( $where_fields ) && is_array( $where_values ) && isset( $where_fields ) && isset( $where_values ) ) { if ( sizeof( $where_fields ) != sizeof( $where_values ) ) { die( "Query error: where <strong>field</strong> count doesn't match <strong>value</strong> count!" ); } else { $where = ' WHERE '; foreach( $where_fields as $key => $value ) { $where .= $key . ' ' . $value . '=? '; } $where = substr( $where, 0, -1 ); echo $query = "SELECT " . $fields . " FROM " . $tables . $where . $order . $limit; $qp = $pdo_db -> prepare( $query ); foreach ( $where_values as $key => $val ) { $qp -> bindParam( $key+1, $val ); } } } else { echo $query = "SELECT " . $fields . " FROM " . $tables . $order . $limit; $qp = $pdo_db -> prepare( $query ); } $qp -> execute(); $result = $qp -> fetchAll(); return $result; } ?> Kura tiek izsaukta šādi: $result = qlist_pdo( "*", "list", array( '' => 'id', 'OR' => 'id' ), array( '2', '8' ), 'id DESC' ); Problēma ir tāda, ka man atgriež vienu rindiņu divu vietā. Ja es to pašu pieprasījumu veicu noņemot where parametrus: $result = qlist_pdo( "*", "list", '', '', 'id DESC' ); Tad es iegūstu ko pieprasīji, respektīvi, visu saturu. Query kas tiek uzgenerēts: SELECT * FROM list WHERE id=? OR id=? ORDER BY id DESC Parametri kuri tiek uzģenerēti: 1, 2 2, 8 Patreiz izskatās, ka tiek atgriezts tikai pēdējais ID, respektīvi, id = 8 tikai nostrādā. Pietam nav starpības, vai es norādu AND vai OR. Tik un tā tiek atgriezts viens rezultāts. Vart paskaidrot kur ir kļūda? Quote
draugz Posted October 3, 2012 Report Posted October 3, 2012 Kods izskatās pareizs :) pamēģini ieslēgt mysql log query :) un paskatīties, kas tad galu galā nonāk līdz sql. Quote
aaxc Posted October 3, 2012 Author Report Posted October 3, 2012 Nu jā, logs uzrādija to par ko es domāju: SELECT * FROM list WHERE id='8' OR id='8' ORDER BY id DESC Kāpēc tie parametri tiek parrakstīti nevis izveidoti papildus? Quote
codez Posted October 3, 2012 Report Posted October 3, 2012 (edited) Vairāku rindu iegūšanai pēc viena parametra parasti izmanto IN SELECT * FROM list WHERE id IN (2,8) Edited October 3, 2012 by codez Quote
aaxc Posted October 3, 2012 Author Report Posted October 3, 2012 PS: Šo pašu jautājumu uzdevu arī stackoverflow, bet bez rezultātiem patreiz. Quote
aaxc Posted October 3, 2012 Author Report Posted October 3, 2012 (edited) Vairāku rindu iegūšanai pēc viena parametra parasti izmanto IN SELECT * FROM list WHERE id IN (2,8) Zinu ka iegūst, man vairāk interesē kāpēc tieši šis testa query neizpildas. Jo ja ar šo jau ir problēmas, kas notiks, ja man vajadzēs uzrakstīt, teiksim, ko šādu? WHERE id > 2 AND id < 8 Edited October 3, 2012 by aaxc Quote
marrtins Posted October 3, 2012 Report Posted October 3, 2012 (edited) Bindojot Tev vajag referenci: foreach ( $where_values as $key => $val ) { uz foreach ( $where_values as $key => &$val ) { Edited October 3, 2012 by marrtins Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.