Jump to content
php.lv forumi

PDO pieprasījums atgriež vienu rindu divu vietā


aaxc

Recommended Posts

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?

Link to comment
Share on other sites

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 by aaxc
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...