0

The following code returns me people with similar telephone numbers. It works perfectly but when there are no numbers the function still returns information meaning that I cannot check hide a certain box if there are no other people with similar numbers.

THE FUNCTION

function getothers($tid,$criteria,$telephone,$telephone2,$elector){
            global $dbh;
            $tid = '-TID'.$tid;
             $sql = "SELECT * FROM electors WHERE ((telephone > 0 AND telephone IN ('$telephone','$telephone2'))  OR (telephone2 > 0 AND telephone2 IN ('$telephone','$telephone2'))) $criteria AND records NOT RLIKE '$tid' AND ID != '$elector'  LIMIT 10";
            $result = $dbh->query($sql);
            return $result;
        }

THE CALL

<?php $others = getothers($post['TID'],$post['criteria'],$elector['telephone'],$elector['telephone2'],$elector['ID']); ?>

THE LINE THAT DOES NOT WORK

<?php if(!$others){?> 

$others still has something in it despite no results. I think I might be missing a line in y PDO. Any ideas?

The print_r

PDOStatement Object ( [queryString] => SELECT * FROM electors WHERE ((telephone > 0 AND telephone IN ('02085414023 ','')) OR (telephone2 > 0 AND telephone2 IN ('02085414023 ',''))) AND (this_vi_street = '' AND this_vi_telephone = '') AND (mosaic IN ('A01','A02','A03','A04','A05','A07','B11','C15','C16','C17','C18','H46','J52','K57','K58','K60') OR last_vi IN ('C','P')) AND postal_vote != 1 AND records NOT RLIKE '-TID1' AND ID != '13' LIMIT 10 )
2
  • 3
    If you're using PDO, please use prepared statements. You're completely foregoing the advantages of PDO and are just using it like your old mysql_ functions. Please see the examples in the manual. Commented Jul 2, 2012 at 13:07
  • Thats probably a good idea. Would you give me just a small hand so I can see it in context of what I am doing. Take my function above. How would it look as a prepared statement. Commented Jul 2, 2012 at 13:21

2 Answers 2

1

As per the comments, a version using prepared statements:

function getothers($tid, $criteria, $telephone, $telephone2, $elector) {
    global $dbh;

    $stmt = $dbh->prepare("SELECT *
                             FROM electors
                            WHERE ((telephone > 0 AND telephone IN (:telephone, :telephone2))
                                   OR (telephone2 > 0 AND telephone2 IN (:telephone, :telephone2)))
                                  $criteria
                                  AND records NOT RLIKE :tid
                                  AND ID != :elector
                            LIMIT 10";

    $stmt->execute(array(
        ':telephone'  => $telephone,
        ':telephone2' => $telephone2,
        ':tid'        => '-TID' . $tid,
        ':elector'    => $elector
    ));

    return $stmt->fetchAll();
}

There are still some bad points in this code:

  • Uses global to get the DB connection, this is overall bad application structure. You should probably use a class or pass $dbh as a regular argument into the function.
  • Concatenates $criteria into the prepared statement. Do you really need such dynamic conditions that you can't prepare a query for it without concatenating whole SQL blocks into it?
  • Doesn't necessarily address your actual problem of function returns.
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe do something like

$result = $dbh->query($sql);
if($result->rowCount()>0)
{
return $result;
}

return false;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.