0

I got a big issue with pdo, noone seems to be able to help me with - so I decided to ask you guys :-)

try {
    $links = $database->prepare("SELECT * FROM aTable WHERE visible=:visible AND access<=:access AND category=:category ORDER BY orderNum ASC");
    $links->bindValue(':visible',$first,PDO::PARAM_INT);
    $links->bindValue(':access',$second,PDO::PARAM_INT);
    $links->bindValue(':category',$third,PDO::PARAM_STR);
    $links->execute();
    print_r($asdf);
    print_r($database->errorInfo());
    print_r($links->errorInfo());
    while($row = $links->fetch(PDO::FETCH_ASSOC)){
        print_r($row);
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}

Database-Connection works perfectly, the errorInfo()'s both return:

Array
(
    [0] => 00000
    [1] => 
    [2] => 
)

Now, this code somehow doesn't get any $row's. But if I would replace the prepare statement with the following lines:

$sql = "SELECT * FROM woody_sidebar WHERE visible=$first AND access<=$second AND category=$third ORDER BY orderNum ASC";
$links = $database->prepare($sql);

(and remove the bindValue-statements) the code works like charm!

I have no clue what I'm doing wrong, because there is no error thrown at all - does anybody of you know anything I could try?

Thanks

Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
  • It works even without quotes around category? In bindValue(), you're saying that category is a string. – Marcus Adams Mar 10 '15 at 17:58
  • Actually not - wrote that line just to show that without bindValue() the execution works perfect and forgot the quotes in the example. The actual sql-string is: `SELECT * FROM \`woody_sidebar\` WHERE \`visible\`=$firstNumber AND \`access\`<=$secondNumber AND \`category\`='$thirdString' ORDER BY \`orderNum\` ASC ` – holzers Mar 10 '15 at 18:21

2 Answers2

0

Change this:

$links = $database->prepare("SELECT * FROM aTable WHERE visible=:visible AND access<=:access AND category=:category ORDER BY orderNum ASC");
$links->bindValue(':visible',$first,PDO::PARAM_INT);
$links->bindValue(':access',$second,PDO::PARAM_INT);
$links->bindValue(':category',$third,PDO::PARAM_STR);

to this:

In the second query your table name is woody_sidebar but you have aTable and $third is a INT and can be passed as a PDO::PARAM_INT

$links = $database->prepare("SELECT * FROM woody_sidebar WHERE visible=:visible AND access<=:access AND category=:category ORDER BY orderNum ASC");
$links->bindValue(':visible',$first,PDO::PARAM_INT);
$links->bindValue(':access',$second,PDO::PARAM_INT);
$links->bindValue(':category',$third,PDO::PARAM_INT);
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0

The only problem I see is your table name.

Try this instead

try {
    $sql = "SELECT * FROM woody_sidebar WHERE visible=:visible AND access<=:access AND category=:category ORDER BY orderNum ASC";
    $links = $database->prepare($sql);
    $links->bindValue(':visible', $first, PDO::PARAM_INT); // assuming this is an integer
    $links->bindValue(':access', $second, PDO::PARAM_INT); // assuming this is an integer
    $links->bindValue(':category', $third, PDO::PARAM_STR); // assuming this is an text string or date
    $links->execute();
    print_r($asdf);
    print_r($database->errorInfo());
    print_r($links->errorInfo());
    while($row = $links->fetch(PDO::FETCH_ASSOC)){
        print_r($row);
    }
} catch (PDOException $e) {
    echo $e->getMessage();
}
Nimantha
  • 6,405
  • 6
  • 28
  • 69
Kuya
  • 7,280
  • 4
  • 19
  • 31