Jump to content

Problem with a PDO db function


Brazzan

Recommended Posts

Having a bit of a problem with looping the bindParam in a foreach. If I only have one statement, the function returns correct data, but if I add another "money => 999" the function returns a empty array. Can't one loop bindParam in this way?

 

Second question is... I would love to scip the second loop and do the bindParam in the first one... possible?

 

<?php 
function dbSelect($table, $query) { 
require_once ("../db/connect.php"); 

    foreach($query as $id => $value) { 
        $dbQuery .= $id." = :".$id.' AND '; 
    } 
    $dbQuery = substr($dbQuery, 0, -5); 
     
    $do = $db_connect->prepare ("SELECT * FROM $table WHERE $dbQuery"); 
    foreach($query as $id => $value) { 
        $do->bindParam(":".$id, $value, PDO::PARAM_INT); 
    } 
    $do->execute(); 
    $result = $do->fetchAll(); 
     
return $result; 
} 

$dbResult = dbSelect("test", 
                $where = array( 
                "id"     => 1, 
                "money" => 999, 
                )     
            ); 

echo "<xmp>".print_r($dbResult, true)."</xmp>"; 
?>

 

If I skip money => 999, the result returns correctly.

 

Array
(
    [0] => Array
        (
            [id] => 1
            [0] => 1
            [name] => Look at me!
            [1] => Look at me!
            [money] => 999
            [2] => 999
            [id2] => 2
            [3] => 2
        )

)

 

But if I add money => 999

Array
(
)

Link to comment
https://forums.phpfreaks.com/topic/162020-problem-with-a-pdo-db-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.