0

So I have been working on this for a number of hours now and I have looked at all the questions with similar names. I have also read the excellent guide here (https://phpdelusions.net/pdo) and the wiki here on SO (https://stackoverflow.com/tags/pdo/info), but I cannot seem to figure out the issue.

I know my connection works as I get expected results when I do not query against one specific field, LIST_105. But as soon as I try to query against LIST_105 I get no results.

Here is how I am connecting:

$options = [];
$this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password, $options);
$this->conn->exec("set names utf8");

I have also tried giving it these options:

$options = array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
);

and I have tried turning on and off emulation $this->conn->setAttribute( PDO::ATTR_EMULATE_PREPARES, false ); but none of those changes have done anything.

My query is super simple:

$query = "SELECT * FROM idx_property WHERE LIST_105 = ?";

// prepare query statement
$stmt = $this->conn->prepare( $query );
$value = '22-1'; <-- I am manually adding this here for testing purposes.
$stmt->bindValue(1, $value, PDO::PARAM_STR);
// execute query
$stmt->execute();

$row = $stmt->fetch();

The query returns false every time. I have also tried using a named placeholder.

$query = "SELECT * FROM idx_property WHERE LIST_105 = :id";
...
$stmt->bindValue(':id', $value, PDO::PARAM_STR);

I have also tried using bindParam and wrapping the $value in an array and passing it directly to the execute method $stmt->execute(array($value)) and nothing works. I can just run the query directly in mysql SELECT * FROM idx_property WHERE LIST_105 = '22-1' and I get the expected result.

I am sure I am missing something obvious, but I can't for the life of me figure out what it is.

Thanks in advance for your help.

MatthewLee
  • 609
  • 1
  • 10
  • 20
  • "always false" means there is no match – Your Common Sense Mar 23 '22 at 17:36
  • 3
    Are you sure your php points to the same copy of the database as the one where you're running the query directly? That could be a reason why the php version doesn't return any rows. – ADyson Mar 23 '22 at 17:38
  • 2
    You said you get the expected result when you query in mysql directly -- do you mean you run the query in the mysql client, not in PHP? I would try it in the PHP app. That is, hard-code your value `'22-1'` in your query in the PHP app and test it. If that doesn't work, but the same hard-coded query works in the mysql client, then I would agree with ADyson that your PHP app is connecting to a different database instance than the one you are testing with the mysql client. This is not an uncommon mistake. – Bill Karwin Mar 23 '22 at 17:45
  • @BillKarwin Thank you, thank you, thank you! That was the stupid error I was making. I was looking at 2 different databases. The production database and my local, out of date, copy! – MatthewLee Mar 23 '22 at 18:35
  • 2
    @ADyson actually you answered it first. So Thank you to you as well, first?!? Anyway, thank you. – MatthewLee Mar 23 '22 at 18:41
  • We've all made that mistake! I like the saying, "an expert is one who has made every mistake in a narrow field." – Bill Karwin Mar 23 '22 at 18:50

0 Answers0